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

%hash = (name => foo, list => (1, 2, 3)); @array = $hash{list}; foreach(@array){print "$_\n";}
Perl seems to ignore the inner parens, 1 is printed and $hash{'2'} is assigned to 3. I'm trying to get a list inside a hash without requiring any extra variables. Is this possible? I tried
%hash = (name => foo, list => \(1, 2, 3)); $ref = $hash{list} @array = @$ref; foreach(@array) { print }
but Perl dies on "Not an ARRAY reference". So, is there any way to put a nameless array inside a hash element without any extra variables? Any help is appreciated.

Replies are listed 'Best First'.
Re: Array inside hashes
by perlmonkey (Hermit) on Jul 22, 2000 at 11:48 UTC
    For an array reference use '[]' instead of '()'
    %hash = (name => foo, list => [1, 2, 3]); $ref = $hash{list}; @array = @$ref; foreach(@array) { print }
    See perldoc perlref
Re: Array inside hashes
by atl (Pilgrim) on Jul 22, 2000 at 11:50 UTC
    Good morning! :-)

    Here's a working script:

    atl@companion:~/perl > cat aih.pl #!/usr/bin/perl -w %hash = (name => foo, list => [1, 2, 3]); foreach( keys %hash ){ print "key: $_, value: $hash{$_}\n"; } $ref = $hash{list}; @array = @$ref; foreach(@array){print "array: $_\n";} atl@companion:~/perl > aih.pl Unquoted string "foo" may clash with future reserved word at ./aih.pl +line 3. key: name, value: foo key: list, value: ARRAY(0x80cf9a4) array: 1 array: 2 array: 3 atl@companion:~/perl >
    Since I didn't have my cup of tea yet (another sign of PM addiction: checking PM before making tea/coffee) I'll just let the code speak. ;-))

    Ok, one comment. From the perlop manpage:

    The => digraph is mostly just a synonym for the comma operator.
    So your code means in essence
    %hash = (name, foo, list, 1, 2, 4);
    Have fun ...

    Andreas

RE: Array inside hashes
by athomason (Curate) on Jul 22, 2000 at 14:03 UTC
    Just a note, \(1, 2, 3) was a good try, but it's actually interpreted as (\1, \2, \3). From perldoc perlref:

    Taking a reference to an enumerated list is not the same as using square brackets--instead it's the same as creating a list of references! @list = (\$a, \@b, \%c); @list = \($a, @b, %c); # same thing! As a special case, `\(@foo)' returns a list of references to the contents of `@foo', not a reference to `@foo' itself. Likewise for `%foo', except that the key references are to copies (since the keys are just strings rather than full-fledged scalars).

    So, as the previous posters mentioned, the solution is to use [1, 2, 3] instead of \(1, 2, 3).

      (Offer your reply needs a preview button :) Thanks everyone for their help! I knew I was missing something obvious. athomason mentioned \($a, @b, %c) is the same as (\$a, \@b, \%c). But is \[$a, $b, $c) the same as [\$a, \$b, \$c]? All the array elements are references, so I believe the first method would be cleaner.
      $a = "foo"; $b = "bar"; $c = "baz"; @list = \[$a, $b, $c]; foreach(@list) { print $$_ . "\n"; }
      Perl appears to set $list[0] to a reference to [$a, $b, $c]. Is there a less redundant way to say [\$a, \$b, \$c]?
      Thanks everyone for their help! I knew I was missing something obvious. athomason mentioned \($a, @b, %c) is the same as (\$a, \@b, \%c). But is \[$a, $b, $c) the same as \$a, \$b, \$c? All the array elements are references, so I believe the first method would be cleaner.
      $a = "foo"; $b = "bar"; $c = "baz"; @list = \[$a, $b, $c]; foreach(@list) { print $$_ . "\n"; }
      Perl appears to set $list[0] to a reference to $a, $b, $c. Is there a less redundant way to say \$a, \$b, \$c?
(kudra) RE: Array inside hashes
by kudra (Vicar) on Jul 22, 2000 at 11:55 UTC
    How do I make a hash of arrays in the Q&A section might have answered your question. But here's an example:
    %hash = (name => foo, list => [1, 2, 3]); print join "\n", (@{$hash{list}}); print "\n${$hash{list}}[1]\n"; # Print 2
    Update: Ignore this. I really need to keep another window open to see who else has answered before hitting submit.