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

I want to make hash accepts two values:

i.e.:

$hash->{'somestring'}[0] = a_num; $hash->{'somestring'}[1] = a_dif_num;
Then I would like to print all of them out.

update (broquaint): added formatting

Replies are listed 'Best First'.
Re: Hash question
by Bilbo (Pilgrim) on Jun 04, 2003 at 13:54 UTC

    Each entry in a hash is a scalar, so it contains just one value. However, this value can be a reference to an array. You should see perlref for details.

    You can put an array reference into your hash using

    $hash{key} = \@my_array; # the \ makes it a reference or $hash{ket} = [1, 23, 37];

    You can put individual entries in your hash using:

    $hash{key}->[0] = 1; $hash{key}->[1] = 23;

    To get them out again use $hash{key}->[0] etc, or get the whole array using @array = @{$hash{key}}.

Re: Hash question
by arthas (Hermit) on Jun 04, 2003 at 14:24 UTC

    You can assign values directly using this syntax:

    $hash{'somestring'}[0] = 13; $hash{'somestring'}[1] = 24;

    To print the data out you can eihter use Data::Dumper or do it by hand (useful if you need to get the data for some particular use of yours):

    foreach my $k(keys %hash) { print "Key: $k\n"; for my $i(0..@{$hash{$k}}-1) { print " - ".$hash{$k}[$i]."\n"; } }

    Michele.

Re: Hash question
by hardburn (Abbot) on Jun 04, 2003 at 13:51 UTC
    use Data::Dumper; print Data::Dumper->Dumper($hash);

    Can't help you further without more info.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated