The output is as below which is not correct#!/usr/bin/perl -w use strict; use warnings; my @data_list = ( {key => 'date', value => ''}, {key => 'correct', value => 'Article_text1'}, {key => 'correct', value => 'Article_text2'}, {key => 'date', value => '2009-01-01'}, {key => 'date', value => '2009-01-02'}, {key => 'date', value => ''} ); my ($correction, $date); foreach my $data_pair(@data_list) { $correction = $data_pair->{value} if exists $data_pair->{value} and # could be undef defined $data_pair->{value} and # at least "" length $data_pair->{value} and # has length $data_pair->{value} and # has true value $data_pair->{key} eq 'correct'; $date = $data_pair->{value} if exists $data_pair->{value} and # could be undef defined $data_pair->{value} and # at least "" length $data_pair->{value} and # has length $data_pair->{value} and # has true value $data_pair->{key} eq 'date'; if($correction && $date) { my $sql .= " INSERT IGNORE INTO table SET text = '".$correction."', date='".$date."'"; print $sql; # Reset for the next loop ($correction, $date) = (undef, undef); } }
Instead the output should beINSERT IGNORE INTO table SET text = 'Article_text2', date='2009-01-01'
The keys and values are always passed in the order as above in the array @data_list How to match the first the text and first date and get the required output.INSERT IGNORE INTO table SET text = 'Article_text1', date='2009-01-01' INSERT IGNORE INTO table SET text = 'Article_text2', date='2009-01-02'
In reply to Alternative values from array and print by gem555
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |