Expansion of variable in foreach

Expansion of variable in foreach

am 19.10.2007 04:02:03 von 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)',10 00,'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

Re: Expansion of variable in foreach

am 19.10.2007 04:30:44 von Gunnar Hjalmarsson

Samik R. wrote:
>
> 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)',10 00,'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.

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Re: Expansion of variable in foreach

am 19.10.2007 13:01:03 von Tad McClellan

Samik R. wrote:

> 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*.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"

Re: Expansion of variable in foreach

am 19.10.2007 18:58:53 von nobull67

On Oct 19, 3:02 am, "Samik R." wrote:
>
> 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)',10 00,'Road')

Your assumption appears to have been false.

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

Re: Expansion of variable in foreach

am 19.10.2007 19:06:06 von nobull67

On Oct 19, 3:02 am, "Samik R." wrote:
>
> 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.