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

Hi Monks,

I have a @t_loop contains the following :

$VAR1 = { 'value_loop' => [ { 'value' => 'Sun' }, { 'value' => ' Mon' }, { 'value' => ' Tue' }, { 'value' => ' Wed' } ], 'label' => 'Dow' };

or

$VAR1 = { 'value' => 'UK', 'label' => 'Country' };

I need to store whole value into a scalar. ie.

$final .=$_->{'label'} . '=' . $_->{'value'} foreach @t_loop;

But, it doesn't store value_loop.

Please guide me.

Thanks in Advance,
Rose

Replies are listed 'Best First'.
Re: store value from AoHoH into a scalar
by ikegami (Patriarch) on Oct 31, 2006 at 07:33 UTC

    You say you have an array (@t_loop), but you dumped a hash ref. If you did print(Dumper(@t_loop)), you should have done print(Dumper(\@t_loop)) so we can see the entire structure. Dumper expects a list of *scalars*.

    Your code is actually already correct for the second dump. The following will handle both data structures you presented:

    foreach (@t_loop) { my $label = $_->{label}; my $value; if (exists($_->{value_loop})) { $value = join(' ', map { $_->{value} } @{$_->{value_loop}); } else { $value = $_->{value}; } $final .= "$label=$value\n"; }

    It's complicated, but that's because your data structure is needlessly complex.

    %t_loop = ( Dow => [ 'Sun', 'Mon', 'Tue', 'Wed' ], );
    %t_loop = ( UK => 'Country', );
    while (my ($key, $val) = each %t_loop) { $val = join(' ', @$val) if ref $val; $final .= "$key=$val\n"; }
    Update: oy! My initial code was very wrong.
Re: store value from AoHoH into a scalar
by planetscape (Chancellor) on Oct 31, 2006 at 16:08 UTC