Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Question about ++ and hash value

by ada (Novice)
on Dec 08, 2007 at 23:38 UTC ( #655898=perlquestion: print w/ replies, xml ) Need Help??
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}++; ?

Comment on Question about ++ and hash value
Select or Download Code
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.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://655898]
Approved by lidden
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2014-11-01 10:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (229 votes), past polls