Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Re: Count and List Items in a List

by ikegami (Pope)
on Nov 01, 2005 at 16:09 UTC ( #504625=note: print w/ replies, xml ) Need Help??


in reply to Count and List Items in a List

while (<FILE>) { @zapschool = split (/\t/); if (m/zaps/i) { print NEWFILE "$zapschool[4]\n"; } }
is slower than
while (<FILE>) { if (m/zaps/i) { @zapschool = split(/\t/); print NEWFILE "$zapschool[4]\n"; } }
which can be made more readable as
while (<FILE>) { if (m/zaps/i) { my $zapschool = (split(/\t/))[4]; print NEWFILE "$zapschool\n"; } }
And the solution is
my %count; while (<FILE>) { if (m/zaps/i) { my $zapschool = (split(/\t/))[4]; ++$count{$zapschool}; } } foreach (keys(%count)) { print NEWFILE "$_: $count{$_}\n"; }


Comment on Re: Count and List Items in a List
Select or Download Code
Re^2: Count and List Items in a List
by Tech77 (Novice) on Nov 02, 2005 at 19:32 UTC
    Hey, This is great! Thank you, and thanks to all the other folks who responded. I did this based on the code:
    open (FILE, "userstab.txt") || die "Cannot open file.\n"; open (NEWFILE, ">results.txt") || die "Cannot find or open file for ed +iting.\n"; my %count; while (<FILE>) { if (m/zaps/i) { my $zapschool = (split(/\t/))[4]; ++$count{$zapschool}; } } foreach (keys(%count)) { print NEWFILE "$_: $count{$_}\n"; } close NEWFILE; close FILE; print "Done!\n";
    WooHoo!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2014-09-21 12:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (168 votes), past polls