Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Foreach loop not looping?!

by rjberry (Novice)
on Oct 09, 2008 at 14:14 UTC ( [id://716216]=perlquestion: print w/replies, xml ) Need Help??

rjberry has asked for the wisdom of the Perl Monks concerning the following question:

I must be doing something incredibly stupid, but my foreach loop isn't working correctly.

print Dumper(@orders); print "\n\n"; foreach (@orders) { print Dumper($_); die(); }

In both the dumps the exact same array is being given back. So it doesn't seem to be splitting it up, just regurgitating it back to me. Anybody have any idea why?

Replies are listed 'Best First'.
Re: Foreach loop not looping?!
by almut (Canon) on Oct 09, 2008 at 14:19 UTC

    Your first dump probably should've been (note the backslash)

    print Dumper(\@orders);

    If you pass an array (instead of an array ref) to Dumper, its individual elements are being dumped, which effectively produces the same output as your explicit looping...

      Ah right, thanks. I've done that but the output hasn't changed at all. I think this is something to do with @orders being taken from XML::Simple, which is apparently a pain in the arse for looping.

        ...but the output hasn't changed at all.

        Seems hard to believe :)  At least, a pair of outer brackets should've been added to the dump. Otherwise, it all depends on what exactly your @orders array contains (one or more elements, etc.).

        Perhaps showing some sample dumps (stripped down) would help shed light on what's going on...

Re: Foreach loop not looping?!
by moritz (Cardinal) on Oct 09, 2008 at 14:20 UTC
    If your arrays @orders contains just one item that is a hash ref you'll get that behaviour.

    With arrays I always use print Dumper(\@array); for clarity.

Re: Foreach loop not looping?!
by GrandFather (Saint) on Oct 09, 2008 at 20:56 UTC

    How about you show us a real sample that we can actually run and that shows the problem you are having in the output?

    If the first and only element in @orders were a reference to an array then what you describe is exactly what I would expect to see. Maybe there is a bug elsewhere in the code that we can't see and your expectations of the content of the orders array are wrong?


    Perl reduces RSI - it saves typing
Re: Foreach loop not looping?!
by injunjoel (Priest) on Oct 10, 2008 at 01:50 UTC
    Does anyone else find the die(); statement strange?

    -InjunJoel
    "I do not feel obliged to believe that the same God who endowed us with sense, reason and intellect has intended us to forgo their use." -Galileo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://716216]
Approved by marto
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (4)
As of 2024-03-28 14:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found