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

References Looping

by CodeJunkie (Monk)
on Apr 06, 2003 at 23:09 UTC ( #248493=perlquestion: print w/replies, xml ) Need Help??

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

Hi Monks
Can anyone tell me how I can loop through this data structure.

I have a structure like this, formed by using XML::Simple to read an XML file. I want to be able to find out how many itemcode values there are in the array.

$config->{bids}->{itemcode}->[0] $config->{bids}->{itemcode}->[1]

Can anyone help me out, i'm not too good with references and finding out how to access the values stored in them.

cheers
Tom

Replies are listed 'Best First'.
Re: References Looping
by VSarkiss (Monsignor) on Apr 06, 2003 at 23:16 UTC
Re: References Looping
by perlplexer (Hermit) on Apr 06, 2003 at 23:14 UTC
    Loop:
    foreach my $item (@{ $config->{bids}{itemcode} }){ print "$item\n"; }
    Size:
    my $size = @{ $config->{bids}{itemcode} };
    --perlplexer
Re: References Looping
by Your Mother (Bishop) on Apr 07, 2003 at 08:28 UTC
    In addition to the great replies above, I'll offer another tidbit. If you aren't sure that your array holds meaningful (defined) information or that it holds unique information (and assuming it needs to), here's something else to try. Not so easy on the eyes, but fun.
    my @unique = keys %{ { map {$_ => 1} grep defined, @{ $config->{bids}{itemcode} } } };
    Then when you take scalar @unique you know that the count represents real values found and only their unique appearance. Drop the keys enclosing block and the map block if you just want to count plain old defined elements of the "itemcode" array.
Re: References Looping
by JamesNewman (Initiate) on Apr 07, 2003 at 18:21 UTC

    Just a couple of extra things to add to the replies; the tutorial section of this site has some good articles about references of particular use might be the "References quick reference" by tye and it maybe easier (to understand at least) if you copy off the array to a proper array, so:-

    my @array = @{$config->{bids}{itemcode}};
    Doing it this way seems to be a bit slower in perl (why?) but would allow you to use simpler looking code later in the function i.e.
    $size = @array; or foreach (@array){ ... }
Re: References Looping
by signal9 (Pilgrim) on Apr 07, 2003 at 21:32 UTC
    I run into a ton of situations like this reporting from databases. You could try handling this like a lookup table:
    foreach my $bids( keys %{ $config } ){ foreach my $itemcode( keys %{ $config->{$bids} } ){ foreach my $inner( @{ $config->{$bids}{$itemcode} } ){ print $inner; # If you just want to dump your vals } } }
    if you like
    Adam

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://248493]
Approved by VSarkiss
Front-paged by diotalevi
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (5)
As of 2020-06-01 23:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?



    Results (12 votes). Check out past polls.

    Notices?