gem555 has asked for the wisdom of the Perl Monks concerning the following question:
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'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Alternative values from array and print
by moritz (Cardinal) on Jul 16, 2009 at 07:08 UTC | |
by gem555 (Acolyte) on Jul 16, 2009 at 07:27 UTC | |
by moritz (Cardinal) on Jul 16, 2009 at 07:59 UTC | |
by gem555 (Acolyte) on Jul 16, 2009 at 09:46 UTC | |
by moritz (Cardinal) on Jul 16, 2009 at 10:57 UTC | |
by gem555 (Acolyte) on Jul 16, 2009 at 07:23 UTC | |
|
Re: Alternative values from array and print
by Jenda (Abbot) on Jul 16, 2009 at 09:43 UTC |