in reply to Re: If condition
in thread If condition

When I print $data_pair->{'key'} gives me all the keys present in the array
When I print $data_pair->{'values'} gives me all the values for the keys
I want to check if $data_pair->{'key'} is equal to "correct" and store the value of $data_pair->{'value'} to $cor.
I want to check if $data_pair->{'key'} is equal to "date" and store the value of $data_pair->{'value'} to $date.

Replies are listed 'Best First'.
Re^3: If condition
by davorg (Chancellor) on Jul 14, 2009 at 11:58 UTC
    When I print $data_pair->{'key'} gives me all the keys present in the array
    When I print $data_pair->{'values'} gives me all the values for the keys

    Your data structure is strange :-)

    I think you need something like this:

    my ($cor, $date); for my $data_pair (@data_list) { if ($data_pair->{key} eq 'correct') { $cor = $data_pair->{value}; } if ($data_pair->{key} eq 'date') { $date = $data_pair->{value}; } } $sql_content .= qq( INSERT IGNORE INTO correct SET text="$cor", date="$date");

    But as others have already mentioned, using DBI placeholders makes far more sense here.

    --

    See the Copyright notice on my home node.

    Perl training courses

Re^3: If condition
by moritz (Cardinal) on Jul 14, 2009 at 12:07 UTC
    Instead of having an array of pairs, you should use a hash. Something along these lines:
    my %h = ( correct => $some_value, date => $other_value, ... ); # later on you can retrieve the data like this, without any iteration: my $date = $h{'date'}; my $cor = $h{'correct'};

    Learn about hashes, they are really worth your time.