Expansion of variable in foreach

Discussion in 'Perl Misc' started by Samik R., Oct 19, 2007.

  1. Samik R.

    Samik R. Guest

    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
    Samik R., Oct 19, 2007
    #1
    1. Advertising

  2. 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)',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.

    --
    Gunnar Hjalmarsson
    Email: http://www.gunnar.cc/cgi-bin/contact.pl
    Gunnar Hjalmarsson, Oct 19, 2007
    #2
    1. Advertising

  3. 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/"
    Tad McClellan, Oct 19, 2007
    #3
  4. 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)',1000,'Road')


    Your assumption appears to have been false.

    Take a look at what's really in @RefineryIDs (etc) .
    Brian McCauley, Oct 19, 2007
    #4
  5. 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.
    Brian McCauley, Oct 19, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Robert Jones

    JSP Custom Tag and forEach variable

    Robert Jones, Jul 9, 2003, in forum: Java
    Replies:
    0
    Views:
    602
    Robert Jones
    Jul 9, 2003
  2. marc wyburn

    variable expansion with sqlite

    marc wyburn, Jul 30, 2008, in forum: Python
    Replies:
    4
    Views:
    351
    marc wyburn
    Jul 30, 2008
  3. pikespeak
    Replies:
    1
    Views:
    498
    Rodrick Brown
    Oct 15, 2010
  4. Joachim Wuttke
    Replies:
    5
    Views:
    93
    Hidetoshi NAGAI
    Oct 4, 2004
  5. Ilias Lazaridis
    Replies:
    24
    Views:
    210
    Ilias Lazaridis
    Jun 22, 2007
Loading...

Share This Page