tallus has asked for the wisdom of the Perl Monks concerning the following question:
At the beginning of the script I read in the relevant data from a file and then add the data collected. This is stored in a hash that contains a list of people that can't attend(value) on any day (key) (in the interests of politeness I want to store thier names so that if there isn't a day where everyone can attend we can at least let people know who can't come)
I then want to create a hash whose key is the day and whose value is the number of people who can't attend. My solution is below but my question is can I do this by using scalar on the hash value directly without creating the unecessary array?
#! /usr/bin/perl -w # %hash1=("key1"=>"value1","key2"=>"value2"); %cant_make_day =( "Mon"=>"Jim", "Tues"=>"Jim, Paul", "Weds"=>"Jim, Paul, Annie" # real people! ); while (($elim_key,$elim_value) = each %cant_make_day) { # is this array necessary? @array= split /,/, $cant_make_day{$elim_key}; $length=@array; # sets value to length of array stored in hash $which_day{$elim_key}=scalar @array; print "The number of people on $elim_key = $which_day{$elim_key}\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Finding the length of an aray in a hash
by gav^ (Curate) on Apr 21, 2002 at 01:11 UTC | |
|
Re: Finding the length of an aray in a hash
by dws (Chancellor) on Apr 21, 2002 at 01:04 UTC | |
by tallus (Initiate) on Apr 21, 2002 at 01:59 UTC | |
|
Re: Finding the length of an aray in a hash
by Fletch (Bishop) on Apr 21, 2002 at 02:15 UTC |