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

#!/usr/bin/perl -w use strict; use warnings; my @data_list = ( {key => 'correct', value => 'Article_text1'}, {key => 'correct', value => 'Article_text2'}, {key => 'date', value => '2009-01-01'}, {key => 'date', value => '2009-01-02'} ); my @correct; my @date; for my $data_pair (@data_list) { print "Value:$data_pair->{value}:Key:$data_pair->{key}\n"; if($data_pair->{key} eq 'correct'){ @correct = $data_pair->{value}; } if($data_pair->{key} eq 'date'){ @date = $data_pair->{value}; } } print "The array is @correct\n"; print "The array is @date\n";
After the if the condition, the values should be pushed to the array
if($data_pair->{key} eq 'correct'){ @correct = $data_pair->{value}; }
I should store all the variables in array and not overwrite after if statement

Replies are listed 'Best First'.
Re: Array values are overwritten
by Fletch (Bishop) on Jul 16, 2009 at 12:16 UTC

    Then perhaps you should push them instead of doing an assignment?

    The cake is a lie.
    The cake is a lie.
    The cake is a lie.

Re: Array values are overwritten
by Anonymous Monk on Jul 16, 2009 at 12:32 UTC
    you must push or unshift the values, not overwrite then.
    if($data_pair->{key} eq 'correct') { @correct = $data_pair->{value}; # overwrite @correct push @correct,$data_pair->{value}; # goes to the end of @correct unshift @correct,$data_pair->{value}; # goes to the front of @correct }