Expansion of variable in foreach

S

Samik R.

Hello,

I have been using perl for quite sometime now, but something got me
stumped. I am trying to use the following code:
--------------------
foreach (@SPointIDs)
{
my $RefID="";
foreach $RefID (@RefineryIDs)
{
$DBHandle->execQueryNoResult("insert into orders
values('$GameID',$CurrentWeek,'$RefID','$_',1000,'Road')");
}
}
------------------

The execQueryNoResult method is expecting a string as it's argument.
When I run this, and print from inside the execQueryNoResult method, the
insert statement comes as follows:
insert into orders
values('D00000',1,'ARRAY(0x1035d5b8)','ARRAY(0x1035d678)',1000,'Road')

But, if I use a for loop rather than a foreach loop, and then access the
elements as $SPointID[$i] etc., then things are fine.

What am I missing here? Is there something wrong in my syntax?

Thanks for any pointers.
-Samik
 
G

Gunnar Hjalmarsson

Samik said:
foreach (@SPointIDs)
{
my $RefID="";
foreach $RefID (@RefineryIDs)
{
$DBHandle->execQueryNoResult("insert into orders
values('$GameID',$CurrentWeek,'$RefID','$_',1000,'Road')");
}
}
------------------

The execQueryNoResult method is expecting a string as it's argument.
When I run this, and print from inside the execQueryNoResult method, the
insert statement comes as follows:
insert into orders
values('D00000',1,'ARRAY(0x1035d5b8)','ARRAY(0x1035d678)',1000,'Road')

In that case, @SPointIDs and @RefineryIDs are not simple arrays, but
arrays of arrays.
But, if I use a for loop rather than a foreach loop, and then access the
elements as $SPointID[$i] etc.,

Do you have a @SPointID variable as well?
then things are fine.

That makes no sense to me.

Please follow the posting guidelines for this group and post a small but
_complete_ script that people can copy and run and that reproduces
similar unexpected output.
 
T

Tad McClellan

Samik R. said:
Is there something wrong in my syntax?


If your program executes, then there is nothing wrong with your syntax.

If your program does not make the output that you expect, then
there is something wrong with your *semantics*.
 
B

Brian McCauley

foreach $RefID (@RefineryIDs)
{
$DBHandle->execQueryNoResult("insert into orders
values('$GameID',$CurrentWeek,'$RefID','$_',1000,'Road')");
}

You appear to be assuming that the values in @RefineryIDs (etc) are
plain strings (and plain strings that contain no characters that are
meta with SQL quoted strings).
print from inside the execQueryNoResult method, the
insert statement comes as follows:
insert into orders
values('D00000',1,'ARRAY(0x1035d5b8)','ARRAY(0x1035d678)',1000,'Road')

Your assumption appears to have been false.

Take a look at what's really in @RefineryIDs (etc) .
 
B

Brian McCauley

my $RefID="";
foreach $RefID (@RefineryIDs)

You are declaring two variables called $RefID with different lexical
scopes. If the loop variable in a for has the same name as an
existing lexically scoped variable a new variable is implicitly
declared[1] with a lexical scope of the loop body. The first $RefID
you set to an empty string and then never use again. This is pure
obfuscation. Instead I suggest you declare just the $RefID that you
actually use.

foreach my $RefID (@RefineryIDs)

[1] I really which Perl would emit a warning when it does this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top