Hello geek12 and welcome to the monstery and to the wonderful world of perl!

as you already get sane and wise answers, I propose you a oneliner solution (change " to ' around the code to run it on Linux):

perl -lane "push @{$H{$F[0]}},$F[1]}{print map{$_,qq( ---> ),scalar @{ +$H{$_}},$/,(join $/,@{$H{$_}}),$/,$/}keys %H" data1.txt 0011 ---> 2 Sally Roy 1122 ---> 2 Brandon Simson 2233 ---> 1 George

You can use -MO=Deparse to have some clue on how to read it:

perl -MO=Deparse -lane "push @{$H{$F[0]}},$F[1]}{print map{ $_,qq( --- +> ),scalar @{$H{$_}},$/,(join $/,@{$H{$_}}),$/,$/ }keys %H" BEGIN { $/ = "\n"; $\ = "\n"; } LINE: while (defined($_ = readline ARGV)) { chomp $_; our @F = split(' ', $_, 0); push @{$H{$F[0]};}, $F[1]; } { print map({$_, ' ---> ', scalar @{$H{$_};}, $/, join($/, @{$H{$_}; +}), $/, $/;} keys %H); } -e syntax OK

See perlrun for perl command line switches, but basically -l takes care of line ending (you are not chomp -ing lines!), -a does autosplit populating the @F array and -n wraps the code in a while loop without printing ( and -p does the same but printing). Braces in the part ..$F[1]}{print.. are a trick named Eskimo Greeting.

See perlvar for $/ and @F

You can explore these switches deparsing them one at time like in ( -e is for execute perl code, and -e 1 is just a null program):

perl -MO=Deparse -l -e 1 BEGIN { $/ = "\n"; $\ = "\n"; } '???'; -e syntax OK perl -MO=Deparse -a -e 1 LINE: while (defined($_ = readline ARGV)) { our @F = split(' ', $_, 0); '???'; } -e syntax OK perl -MO=Deparse -n -e 1 LINE: while (defined($_ = readline ARGV)) { '???'; } -e syntax OK perl -MO=Deparse -p -e 1 LINE: while (defined($_ = readline ARGV)) { '???'; } continue { die "-p destination: $!\n" unless print $_; } -e syntax OK
L*

There are no rules, there are no thumbs..
Reinvent the wheel, then learn The Wheel; may be one day you reinvent one of THE WHEELS.

In reply to Re: Frequency occurence of same words in a file -- oneliner by Discipulus
in thread Frequency occurence of same words in a file by geek12

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.