Here is the killer question. I have files relating to the 24 hours of the day, so i wish to loop them by file name $hour = 00 = 01 = 02 etc up to 23,
That part's very easy. Use a foreach loop with the range operator:
foreach my $hour (map { sprintf "%02d", $_ } 0 .. 23) { # Do Stuff Here }
and i want to take all the data out of each file and put it into a hash.
So within the foreach loop, you will open the file for that hour and then read from the file using a the angle-brackets operator, like <FILEHANDLE>, probably repeatedly (e.g., using a while loop), parse the result, and store it in a hash. The parsing part is going to depend somewhat on what your data looks like, but most simple assignments like this can be done with split or a simple regular expression pattern match. Then store it in your hash like this: $hash{$key} = $value; If you need more help with that part, you'll have to give us more information about what your data looks like, which part is the hash key, and which part is the value.
It would also be possible to do the thing as a list transformation, feeding the list-context file read through map and into the hash, but for a beginner the other way is probably easier.
But i want it to just keep one of each unique type.
I'm not sure what the word "type" means in this sentence, but if you use a string representation of the type as the hash key, then you'll end up with just one value for each key, because that's what hashes do. If you assign each value you run into, the hash will remember the last one assigned. If you want the first one, you can make the assignment contingent on there not already being a value, like so: $hash{$key} = $value unless exists $hash{$key}; That way you'd only get the first value for each key.
My files contain "perl.exe , svchost.exe etc"
I'm not sure exactly what this means. Do you mean that you have perl installed on your computer? If so, I think we sort of assumed that would be the case if you're writing programs in Perl.
I can do this, however i wish to increment the keys() of each value when it comes across a duplicate
I am not at all sure what you mean by this. Storing a value in a hash using a key that hadn't already been used will cause keys() to return a value one larger than before. Is your key coming out of the data you're parsing, or do you need to make up a key for each line? One way to do that would be to count the lines (using a counter variable that you add one to each time you read a line) and use (a stringification of) that number as the hash key for the data you just read. (You don't have to manually stringify the number; perl will do that automatically when you use it as a hash key.)
In reply to Re: Perl Hash Files
by jonadab
in thread Perl Hash Files
by blundell
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |