This smells like homework, so you can use this working solution if you can explain or re-write it.
echo "Student Id Class
John 23 2
Mark 33 3
Jerry 44 2
Sam 55 2
Tom 65 3
" | perl -ane '$F[2]=~/^\d+$/ or next;push @{$h[$F[2]]},$F[0]}{ $h[$_]
+ && print qq|Class $_ = |,join(",",@{$h[$_]}),qq|\n| for 0..$#h'
Output:
Class 2 = John,Jerry,Sam
Class 3 = Mark,Tom
UPDATE: FWIW, the Data structure used in the code above is an "Array of Array(ref)s".
The code posted below this node uses a Hash of array refs.
"Software interprets lawyers as damage, and routes around them" - Larry Wall
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.