Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

foreach not doing what I expect it to be doing

by skorman00 (Initiate)
on Feb 08, 2006 at 21:06 UTC ( [id://528934]=perlquestion: print w/replies, xml ) Need Help??

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

I'm a level 10 C/C++ coder, level 2 ASM coder, and now need to tri-class with Perl. Here is a bit of code I'm having trouble with:
my @bucketList = $_xmlData->{Bucket}; open(DEBUG, ">bucketList.txt"); print DEBUG Dumper(@bucketList); close(DEBUG); foreach my $bucket ( @bucketList ) { open(DEBUG, ">bucket.txt"); print DEBUG Dumper($bucket); close(DEBUG); }
$_xmlData holds the data returned from an XML::Simple::XMLin call, Bucket being the key that references an array of all these things called buckets. Dumper is a function in Data (not sure if it's a commonly used tool, but it seems to print out arrays and hashes nicely). Trying to handle this with a foreach loop, I put all that data into @bucketList. The dump shows that it is all ok. The problem is with my foreach variable, $bucket. I would think that it would be set to each element of @bucketList sequentially. According to the dump, it's actually set to the entire array. What am I doing wrong? as requested, here are snippets of the dumps: bucketList:

$VAR1 = [ { 'ExeVersion' => NDA_BLOCKED, 'ModuleVersion' => NDA_BLOCKED, 'ResponseStatus' => NDA_BLOCKED, 'ExeName' => NDA_BLOCKED, 'Priority' => NDA_BLOCKED, 'TotalHits' => NDA_BLOCKED, 'ResponseURL' => NDA_BLOCKED, 'ModuleName' => NDA_BLOCKED, 'BucketID' => NDA_BLOCKED' }, { 'ExeVersion' => NDA_BLOCKED, 'ModuleVersion' => NDA_BLOCKED, 'ResponseStatus' => NDA_BLOCKED, 'ExeName' => NDA_BLOCKED, 'Priority' => NDA_BLOCKED, 'TotalHits' => NDA_BLOCKED, 'ResponseURL' => NDA_BLOCKED, 'ModuleName' => NDA_BLOCKED, 'BucketID' => NDA_BLOCKED' }, { 'ExeVersion' => NDA_BLOCKED, 'ModuleVersion' => NDA_BLOCKED, 'ResponseStatus' => NDA_BLOCKED, 'ExeName' => NDA_BLOCKED, 'Priority' => NDA_BLOCKED, 'TotalHits' => NDA_BLOCKED, 'ResponseURL' => NDA_BLOCKED, 'ModuleName' => NDA_BLOCKED, 'BucketID' => NDA_BLOCKED' }]

bucket:

$VAR1 = [ { 'ExeVersion' => NDA_BLOCKED, 'ModuleVersion' => NDA_BLOCKED, 'ResponseStatus' => NDA_BLOCKED, 'ExeName' => NDA_BLOCKED, 'Priority' => NDA_BLOCKED, 'TotalHits' => NDA_BLOCKED, 'ResponseURL' => NDA_BLOCKED, 'ModuleName' => NDA_BLOCKED, 'BucketID' => NDA_BLOCKED' }, { 'ExeVersion' => NDA_BLOCKED, 'ModuleVersion' => NDA_BLOCKED, 'ResponseStatus' => NDA_BLOCKED, 'ExeName' => NDA_BLOCKED, 'Priority' => NDA_BLOCKED, 'TotalHits' => NDA_BLOCKED, 'ResponseURL' => NDA_BLOCKED, 'ModuleName' => NDA_BLOCKED, 'BucketID' => NDA_BLOCKED' }, { 'ExeVersion' => NDA_BLOCKED, 'ModuleVersion' => NDA_BLOCKED, 'ResponseStatus' => NDA_BLOCKED, 'ExeName' => NDA_BLOCKED, 'Priority' => NDA_BLOCKED, 'TotalHits' => NDA_BLOCKED, 'ResponseURL' => NDA_BLOCKED, 'ModuleName' => NDA_BLOCKED, 'BucketID' => NDA_BLOCKED' }]

Code tags added by Arunbear

Replies are listed 'Best First'.
Re: foreach not doing what I expect it to be doing
by revdiablo (Prior) on Feb 08, 2006 at 21:19 UTC

    You are creating an array with only a single element. That single element happens to be an array reference. So the foreach loop is working how you expect, it's your earlier assignment that isn't. You might have better luck by dereferencing the arrayref on assignment (note, this makes a copy. Depending on the circumstances, that may be bad):

    my @bucketList = @{ $_xmlData->{Bucket} }; ...

    See also perlreftut for a nice references tutorial, or perlref for the full reference docs.

      Also, but not related to your problem, you should open your bucket.txt only once, otherwise it'll be overwritten on each iteration.
      open(DEBUG, ">bucket.txt"); foreach my $bucket ( @bucketList ) { print DEBUG Dumper($bucket); } close(DEBUG);
      -- 6x9=42
        That, or open it with >> instead of >. But your solution is better overall, I am just stating a TIMTOWTDI.

        Update: Mental note, read all replies before responding!
Re: foreach not doing what I expect it to be doing
by olus (Curate) on Feb 08, 2006 at 21:33 UTC
    Aside from what revdiablo said you should note that you rewriting the file each time you iterate the foreach loop.
    use
    open(DEBUG, ">>bucket.txt");
    instead (note de two '>>').
    This way you will be appending Dumper results to the file instead of rewriting it.
    Data::Dumper is your friend (c:
    I use a lot.
Re: foreach not doing what I expect it to be doing
by Anonymous Monk on Feb 09, 2006 at 13:27 UTC
    Dumper is a function in Data
    Dumper is an exported function in the module Data::Dumper, thus the full qualified name is Data::Dumper::Dumper().

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-19 07:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found