Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: Counting within an array

by happy.barney (Friar)
on Nov 30, 2010 at 13:48 UTC ( [id://874496]=note: print w/replies, xml ) Need Help??


in reply to Counting within an array

use hash:
my %hash; while (<MYINPUTFILE>) { chomp; $hash{$_} ++; } $\ = "\n"; while (my ($k, $v) = each %map) { print $k, $v; }

Replies are listed 'Best First'.
Re^2: Counting within an array
by kennethk (Abbot) on Nov 30, 2010 at 14:04 UTC
    In order to meet the posted spec (output on every line), you can move the print statement into the while loop:

    my %hash; while (<MYINPUTFILE>) { chomp; $hash{$_} ++; print "$_$hash{$_}\n"; }
      That works like a charm, thank you very much.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://874496]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (4)
As of 2025-07-09 00:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.