Re: If condition
by moritz (Cardinal) on Jul 14, 2009 at 11:20 UTC
|
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?. | [reply] [d/l] [select] |
|
|
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.
| [reply] |
|
|
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.
| [reply] [d/l] |
|
|
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. | [reply] [d/l] |
Re: If condition
by psini (Deacon) on Jul 14, 2009 at 11:22 UTC
|
If you correctly indent your code, you'll see that the second if (date) is inside then block of the first if (correct). Obviously $data_pair->{'key'} cannot be equal to 'correct' and 'date' at the same time, so there is something wrong in your logic.
If you could tell us what are you trying to achieve, answering will be simpler.
Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."
| [reply] |
Re: If condition
by davorg (Chancellor) on Jul 14, 2009 at 11:20 UTC
|
Can you please tell me what is the problem in above conditions.
Hard to be sure without knowing more about your data. What unexpected behaviour are you seeing? Do you really have hashes with keys called "key" and "value"?
How to store the value of $cor and $date oustide the if condition
Store the values in variables that are declared outside of the if block.
| [reply] |
Re: If condition
by jeanluca (Deacon) on Jul 14, 2009 at 11:23 UTC
|
this smells like a scoping problem. Did you use
use strict ;
use warnings ;
at the beginning of you program. This is the idea
{
my $var = 10 ;
}
print "$var\n" ;
which doesn't print 10. But this does
my $var ;
{
$var = 10 ;
}
print "$var\n" ;
Hope this helps! | [reply] [d/l] [select] |
|
|
If I use this way on the loop, all the values will be null
| [reply] |
|
|
you should do something like
my $cor ;
my $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 .= "INSERT IGNORE INTO correct SET text='".$cor."',
+ date='".$date."';
}
I'm not sure if this is what you want, but it makes more sense to me!
And as mentioned already, use placeholders!!
UPDATE: Hmmm, this doesn't make much sense too :) but I hope it helps! | [reply] [d/l] |
|
|
Re: If condition
by ropey (Hermit) on Jul 14, 2009 at 13:30 UTC
|
I can only presume you are waiting for both 'date' and 'correct' to be true before inserting into the DB.
I would agree with earlier comments about your SQL, and ask why you are creating it in a variable and not inserting it? much better to add placeholders...
Presuming you are doing what I think you are, and are waiting for both the date and 'correct' to be true this may help
#!/usr/bin/perl -w
use strict;
use warnings;
my @data_list = (
{key => 'date', value => '2009-01-01'},
{key => 'correct', value => 'some_data_here'},
{key => 'date', value => '2009-01-02'},
{key => 'correct', value => 'some_data_here_again'}
); # Presumption of your test data based on your code..
my ($cor, $date);
foreach my $data_pair(@data_list) {
$cor = $data_pair->{value} if($data_pair->{key} eq 'correct');
$date = $data_pair->{value} if($data_pair->{key} eq 'date');
if($cor && $date) {
# SQL Update goes here....
print "Found a match for $date and $cor\n";
# Reset for the next loop
($cor, $date) = (undef, undef);
}
}
| [reply] [d/l] |
Re: If condition
by mzedeler (Pilgrim) on Jul 14, 2009 at 18:42 UTC
|
You need to indent your code. Not indenting correctly makes it hard to see what really happens. This is how it should look:
for my $data_pair (@data_list) {
if($data_pair->{'key'} eq 'correct') {
my $cor= $data_pair->{'value'};
if($data_pair->{'key'} eq 'date') {
my $date = $data_pair->{'value'};
...
}
}
}
It is now clear that getting to the my $date = $data_pair ... is impossible.
Another issue is that you have a syntax error in your script. I can't see how you'd ever be able to run it:
$sql_content .= "
INSERT IGNORE INTO correct SET
text='".$cor."',
date='".$date."';
Again, formatting the code better and using DBI placeholders or just interpolation would help you see it (interpolation: you can write text='$cor' in stead of text='" . $cor . "'.
So my message is: keeping your code tidy is a great way of avoiding the kind of confusion that caused you to ask here in the first place.
| [reply] [d/l] [select] |