How about going surgical instead of clinical? suppose you got 150 wrestlers names, and that you're supposed to sort them alphabetically and you wanted to treat the combination of the wrestler's name, the cheer, the killer move as well as any other additional parameters as a unified data structure that can be expanded to contain other information about the same wrestler, examples of these data can be descriptions about his age, weight in pounds, winning matches, lost games, the association he's member of, a yes/no thingy if he was kicked in the butt by the Undertaker...etc.
Consider getting an insight into References, they're so powerful. Not belittling the other solutions here by toolic and ww for they are far more readable and constructed and these guys are more experienced than humble me, but I am showing you a TIMTOWTDI thing cuz a surgeon needs to know what there in the guts is :p
Here we go:
#!/usr/local/bin/perl
use strict;
use warnings;
my %hash;
while(<DATA>){
chomp;
my ($name,$crowdReaction,$killerMove)=split /\|/;
$hash{$name}=[] unless exists $hash{$name}; #to skip duplicate
+ wrestlers names just in case.
push @{$hash{$name}}, $crowdReaction,$killerMove;
}
print "WRESTLER\tCrowd Reaction\tKiller Move\n";
print "_" x 50, "\n";
foreach my $name (sort keys %hash){
for(my $i=0;$i< length(@{$hash{$name}});$i++){
print "$name\t@{$hash{$name}}[$i]\t\t@{$hash{$name}}[+
++$i]\n";
}
}
__DATA__
The Rock|Cheer|Rock Bottom
Triple H|Boo|Pedigree
Stone Cold|Cheer|Stone Cold Stunner
WRESTLER Crowd Reaction Killer Move
__________________________________________________
Stone Cold Cheer Stone Cold Stunner
The Rock Cheer Rock Bottom
Triple H Boo Pedigree
N.B: The way you used the open function to open a file, you haven't provided arguments specifying if the file has been opened in modes of input, output, append...
Excellence is an Endeavor of Persistence.
Chance Favors a Prepared Mind.
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.