in reply to Re: Perl Hash Files
in thread Perl Hash Files

ok so i have
$hour = "00"; while ($hour < 25) { $infile = "$hour.txt"; open (LOG, $infile) while <LOG> { #PUT EACH NEW LINE INTO THE HASH } }
sorry by "perl.exe" i mean each line of my text files holds a process the computer is doing do
"SNDSrvc.exe SPBBCSvc.exe symlcsvc.exe spoolsv.exe AluSchedulerSvc.exe svchost.exe btwdins.exe"
And i am trying to make my hash hold
%hash = ("btwdins.exe", 1, "spoolsv.exe", 1, "svchost.exe", 4,);
So it takes all the lines from the text files but increments the value for every duplicate.
So if svchost.exe is in four text files, or four times in one file, its key and value would be "svchost.exe", 4.

My txt files are already parsed as each *.exe is on a new line. I hope this helps and is not to complicated. Any help is much much appreciated, my brain is frazzled.

Replies are listed 'Best First'.
Re^3: Perl Hash Files
by cdarke (Prior) on Dec 11, 2006 at 18:28 UTC
    Detecting duplicates are what Tiggers and hashes do best:
    while (<LOG>) { # Assume name of program is in $_ chomp; $hash{$_}++; }
    The hash key will be created and the value set to undef (zero), then incremented on the first time around. Subsequent occurances of the same key will increment the value.

    By the way, a small thing, but be careful when you use a leading zero on a numeric - it converts it to octal.
      thanks i have to use leading zero's because my text files are named 01 02 03 04, have to look for a way, can't i tell it not to change to octal? Do you mean it changed the hask key to octal?
        Do you mean it changed the hask key to octal?

        No. Hash keys and filenames are strings, not numbers, so the leading zero does nothing in those cases and has no impact on your program. (Quite aside from that, you also have only one digit after the zero, so these numbers would be the same in octal as in decimal, except for 08 and 09, which aren't even valid in octal and would therefore still be interpreted as decimal, so there's another reason it has no impact.) He was just making you aware for future reference that if you have something like '024' and then treat it like a number (e.g., by adding another number to it), the results may not be quite what you might expect. That's probably why he called it a "small thing", because it doesn't have any impact on the operation of your current program.


        Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.
Re: Perl Hash Files
by jonadab (Parson) on Dec 12, 2006 at 12:41 UTC

    You forgot to increment $hour:

    $hour = "00"; while ($hour < 25) { $infile = "$hour.txt"; open LOG, '<', $infile; while <LOG> { #PUT EACH NEW LINE INTO THE HASH } $hour++; }

    That's one reason I'd suggested a foreach loop, because it takes care of that automatically. It also takes care of the initial assignment, too. But the while loop will get the job done also, with this addition.

    And i am trying to make my hash hold %hash = ("btwdins.exe", 1, "spoolsv.exe", 1, "svchost.exe", 4,); So it takes all the lines from the text files but increments the value for every duplicate.

    Ah, I see. So the strings that come out of the hour files (one per line) are themselves filenames, and you want to use those as the hash keys, and make the value be the count of the number of times they occur? In that case, you can just increment the value each time, as cdarke suggests.


    Sanity? Oh, yeah, I've got all kinds of sanity. In fact, I've developed whole new kinds of sanity. You can just call me "Mister Sanity". Why, I've got so much sanity it's driving me crazy.