tallus has asked for the wisdom of the Perl Monks concerning the following question:

I am trying to organize a get together of people from my singing group using a CGI script. The optimum solution is to find the day where most people can attend from a choice of the days given.

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
    You could just do:
    %which_day = map { $_ => scalar split /,/, $cant_make_day{$_}} keys %cant_make_day;
    You should really be using strict and declaring your variables with my. This can save you headaches with misspelt names (see this node) Also you might want to use $elem rather than $elim (as it is short for element.

    Hope this helps.

    gav^

Re: Finding the length of an aray in a hash
by dws (Chancellor) on Apr 21, 2002 at 01:04 UTC
    Can I do this by using scalar on the hash value directly without creating the unecessary array?

    Given the way the data is organized, you can avoid the temporary array by counting the number of commas in a (hash) value then adding 1.

    Instead of

    @array = split /,/, $cant_make_day{$elim_key}; $length = @array;
    try   $length = 1 + $cant_make_day{$elim_key} =~ tr/,/,/; This assumes that you never allow an empty ("") value in your hash.

      This appeals to me but I don't think I can avoid an empty hash - the optimal solution is a day which no one has any problems attending (no one == an empty hash)
Re: Finding the length of an aray in a hash
by Fletch (Bishop) on Apr 21, 2002 at 02:15 UTC

    Just use a proper data structure and you don't have to worry about splitting or temporary arrays.

    my %cmd = ( Mon => [ qw( Jim ) ], Tue => [ qw( Jim Paul ) ], Wed => [ qw( Jim Paul Annie ) ], ); print scalar @{ $cmd{ $_ } }, " can't make it on $_\n" foreach keys %cmd;

    perldoc perldsc, perldoc perllol, perldoc perlreftut

    But to answer the original question, no it's not necessary. split in scalar context does the right thing.