Currently my code is printing $id and # of times $id is in @data2.
for ( @data ) {
my ($id,$name,$ref) = split /,/;
# You may wish to add some error checking to make sure the
# hash key $id does not already exist
$keyhash{$id} = { NAME => $name,
REF => $ref#,
#MANY => [],
}
}
for ( @data2 ) {
my ($id,$name,$ref) = split /,/;
# Warn and do nothing if a record is found for which the
# $id is not already in %keyhash
unless ( defined( $keyhash{$id} ) ) {
warn "No such record $id!\n";
next;
}
push @{$keyhash{$id}{MANY}}, [ $name, $ref ];
}
##Although you may want MANY to be a hash - it really depends on how y
+ou want to use your data later.
##Finally, to extract the number of records for each ID,
##print header
print "Customer #of docs\n";
# A little something to get the plurality correct
for ( keys %keyhash ) {
my $num = @{$keyhash{$_}{MANY}};
printf "%s appeared %d %s\n",
$_,
$num,
$num > 1 ? "times" : "time";
}
When the script is executed it prints:
No such record 4!
No such record 5!
Customer #of docs
appeared 0 time
22 appeared 0 time
1 appeared 3 times
2 appeared 3 times
3 appeared 4 times
How can I print $name instead of $id? How can I get other variables if added to the array?
Thank you.
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.