http://www.perlmonks.org?node_id=655898

ada has asked for the wisdom of the Perl Monks concerning the following question:

Hi perl peeps, I have this code to find the numbers of numbers for each entry:

my %hash; while(my $line=<>){ chomp $line; chop $line; $hash{$line}++; } print %hash;

My Q is what goes on here : $hash{$line}++; ?

Replies are listed 'Best First'.
Re: Question about ++ and hash value
by grep (Monsignor) on Dec 08, 2007 at 23:46 UTC
    ++ is the auto-increment operator.
    $hash{$line} is a hash assignment

    So $hash{$line}++ auto-increments the value of $hash{$line}

    Also please read Writeup Formatting Tips and use <code> tags around your code

    grep
    One dead unjugged rabbit fish later...
      Ok thanks, so I guess that each value increases by how many occurances there are of the key in the hash. Is this the understanding?
Re: Question about ++ and hash value
by swampyankee (Parson) on Dec 09, 2007 at 00:32 UTC

    It's counting the number of times a given line appears in a file. $hash{$line}++ is incremented by 1 (set to one automagically on the first instance of $line) each time $line (well, $line minus the piece removed by chop) appears.


    emc

    Information about American English usage here and here.

    Any Northeastern US area jobs? I'm currently unemployed.

      Ok thanks so are these increments stored as values of keys? Ada x

        Yes


        emc

        Information about American English usage here and here.

        Any Northeastern US area jobs? I'm currently unemployed.