in reply to If condition

The problem is that $data_pair->{'key'} can't be equal to 'correct' and 'date' at the same time.

It looks like you should really use a hash instead, and simply look up the values by key instead.

Oh, and please don't interpolate values into SQL, use placeholders instead: What are placeholders in DBI, and why would I want to use them?.

Replies are listed 'Best First'.
Re^2: If condition
by gem555 (Acolyte) on Jul 14, 2009 at 11:33 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
    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.
      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

      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.