#!/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); } } #### INSERT IGNORE INTO table SET text = 'Article_text2', date='2009-01-01' #### INSERT IGNORE INTO table SET text = 'Article_text1', date='2009-01-01' INSERT IGNORE INTO table SET text = 'Article_text2', date='2009-01-02'