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

I have the following data structure from Data::Dumper
$VAR1 = { 'last' => [ 'jones', 'smith', 'long' ] };
How do I print the contents of the array?

I tried the following:

foreach my $code ( keys %{$info} ) { print $code . ":"; foreach my $last ( @{$code} ) { print " " . $last; } print "\n"; }
I think I'm close...and $info is a scalar so I'm assuming I have to use %{$info}

Thanks

Replies are listed 'Best First'.
Re: printing hash of array
by xdg (Monsignor) on Dec 06, 2006 at 02:59 UTC

    This should do:

    print "$_: @{ $info->{$_} }\n" for keys %{ $info };

    -xdg

    Code written by xdg and posted on PerlMonks is public domain. It is provided as is with no warranties, express or implied, of any kind. Posted code may not have been tested. Use of posted code is at your own risk.

      worked Perfect!...thanks
Re: printing hash of array
by wjw (Priest) on Dec 06, 2006 at 03:31 UTC
    You might find the following helpful....

    Doc

    ...the majority is always wrong, and always the last to know about it...

Re: printing hash of array
by liverpole (Monsignor) on Dec 06, 2006 at 15:09 UTC
    You were very close.

    Here's the final piece that you needed, which was to dereference the hash info using the value of the key $code:

    use strict; use warnings; + my $info = { 'last' => [ 'jones', 'smith', 'long' ] }; + + foreach my $code ( keys %{$info} ) { print $code . ":"; foreach my $last ( @{$info->{$code}} ) { print " " . $last; } print "\n"; }

    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/