Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: counting lines in perl

by davidj (Priest)
on Feb 27, 2005 at 05:21 UTC ( [id://434858]=note: print w/replies, xml ) Need Help??


in reply to counting lines in perl

This is failry concise:

#!/usr/bin/perl use strict; my (%words, $key); open(FILE, "<test.txt"); while(<FILE>) { chomp($_); $words{$_}++; } close(FILE); foreach $key (keys %words) { print "$words{$key} $key\n"; } exit;


davidj

Replies are listed 'Best First'.
Re^2: counting lines in perl
by chas (Priest) on Feb 27, 2005 at 06:30 UTC
    But that code doesn't seem to count groups of consecutive repetition just once, does it? - (which is what I thought the original poster wanted.)
    chas
    (Update: Actually, now that I've gone to a system where I could try out uniq -c, I see that I misunderstood what was desired so my code doesn't seem to do what the original poster wanted. Your code is closer, but the output isn't the same as that of uniq -c, at least the version I used. Sorry about the confusion...)
      You are correct. My code is flawed. For some reason I thought uniq -c sorted the file first, but it doesn't. My mistake.

      davidj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2024-04-18 23:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found