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

Unique items in a text file

by Anonymous Monk
on Apr 20, 2004 at 14:08 UTC ( #346636=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi There;
How can generate a html report from this text file to show all the unique items on the file? I could use split, but after that I am getting lost trying to count the unique item, like:
I would like to show something like,
Companies: 11<br> FA: 2 <br> DI: 2 <br> DX:1 <br> Login: 2 <br>

Thanks for the help!
____DATA Auto,00883,pw00,DX,4/15/2004,10:12:59,1110046,1 Auto,00883,pw00,FA,4/15/2004,10:15:45,, Magic,006513,pw13,Login,4/15/2004,10:16:23,, Car Wash,006513,pw3h,Not Found,4/15/2004,10:16:57,1232246, Home and Auto,006513,pw6513,DI,4/15/2004,10:17:09,15134346,1 Auto,00883,pw00,FA,4/15/2004,10:17:17,, Home,006513,pw6513,Login,4/15/2004,10:17:38,, Home,006513,pw6513,DI,4/15/2004,10:17:44,151234326,1 Deal Time,006513,pw001,Login,4/15/2004,10:53:31,1, Car Wash,006513,pw3h,DI,4/15/2004,10:53:46,11238748,1 Magic,006513,pw13,Login,4/15/2004,11:01:25,0, ___DATA

Comment on Unique items in a text file
Select or Download Code
Re: Unique items in a text file
by BazB (Priest) on Apr 20, 2004 at 14:22 UTC
    perldoc -q duplicate

    If the information in this post is inaccurate, or just plain wrong, don't just downvote - please post explaining what's wrong.
    That way everyone learns.

Re: Unique items in a text file
by Tomte (Priest) on Apr 20, 2004 at 14:25 UTC

    Use a hash, something like the following code

    my %erg = (); while(<DATA>) { my @row = split(/,/,$_); ++$erg{$row[3]}; } print $_ . ': ' . $erg{$_} . '<br />' . "\n" foreach (keys %erg); __DATA__ Auto,00883,pw00,DX,4/15/2004,10:12:59,1110046,1 Auto,00883,pw00,FA,4/15/2004,10:15:45,, Magic,006513,pw13,Login,4/15/2004,10:16:23,, Car Wash,006513,pw3h,Not Found,4/15/2004,10:16:57,1232246, Home and Auto,006513,pw6513,DI,4/15/2004,10:17:09,15134346,1 Auto,00883,pw00,FA,4/15/2004,10:17:17,, Home,006513,pw6513,Login,4/15/2004,10:17:38,, Home,006513,pw6513,DI,4/15/2004,10:17:44,151234326,1 Deal Time,006513,pw001,Login,4/15/2004,10:53:31,1, Car Wash,006513,pw3h,DI,4/15/2004,10:53:46,11238748,1 Magic,006513,pw13,Login,4/15/2004,11:01:25,0,
    Edit: fixed missing right-curly.
    Edit II: fixed spelling error, removed untested-remark.

    regards,
    tomte


    An intellectual is someone whose mind watches itself.
    -- Albert Camus

      Hi,
      I run the code and it is printing back the total for all the item just like the problem I was having before, but thanks for your effort.
      Result prints:
       48

        Dunno, that's what I get:

        DI: 3<br /> Not Found: 1<br /> FA: 2<br /> Login: 4<br /> DX: 1<br />

        regards,
        tomte


        An intellectual is someone whose mind watches itself.
        -- Albert Camus

        I got the same here, how would you do if you had to add this numbers up instead of displaying them in order?

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2014-08-01 06:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite superfluous repetitious redundant duplicative phrase is:









    Results (257 votes), past polls