Dave Chappell wrote:
> 
> Hi,

Hello,

> I have a hash that will have some keys contain single values and other keys
> that will have multiple values:
> 
> %hash = (
>     key1 => 'test-1',
>     key2 => ['test-2-0', 'test-2-1'],
>     key3 => 'test-3'
> );

The simple answer is to put all values in arrays.  :-)

my %hash = (
    key1 => [ 'test-1' ],
    key2 => [ 'test-2-0', 'test-2-1' ],
    key3 => [ 'test-3' ],
);



> I want to loop through the hash getting the value(s) from each key. When I
> try this with the code below, key2 returned something different.
> 
> foreach $key(keys %hash) {
>     print "$key = $hash{$key}\n";
> }
> 
> screen output:
> 
> key1 = test-1
> key2 = ARRAY(0x1bdf0b4)
> key3 = test-3


foreach my $key ( keys %hash ) {
    if ( ref $hash{$key} ) {
        print "$key = @{$hash{$key}}\n";
    }
    else {
        print "$key = $hash{$key}\n";
    }
}


John
-- 
use Perl;
program
fulfillment

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to