Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:
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"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: hash me a few variables
by busunsl (Vicar) on Mar 20, 2001 at 04:10 UTC | |
|
Re: hash me a few variables
by buckaduck (Chaplain) on Mar 20, 2001 at 04:15 UTC | |
by Anonymous Monk on Mar 20, 2001 at 04:20 UTC | |
by Anonymous Monk on Mar 20, 2001 at 04:23 UTC | |
by buckaduck (Chaplain) on Mar 20, 2001 at 04:46 UTC |