Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Loop through hash structure

by PerlScholar (Acolyte)
on Sep 06, 2010 at 10:18 UTC ( [id://859035]=perlquestion: print w/replies, xml ) Need Help??

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

UPDATED

Hi All, need some help looping through a hash structure generated by XML::Simple config. So far this is what I have:

use strict; use XML::Simple; use Data::Dumper; my $config = XMLin('config.xml'); print Dumper($config); for my $record (@{my $VAR1->{Application}}) { my $environment = $record->{Environment}->{Name}; my $region = $record->{Environment}->{Region}->{Name}; my $Idrive = $record->{Environment}->{Region}->{Idrive}; my $Zdrive = $record->{Environment}->{Region}->{Zdrive}; print "$environment"; print "$region"; print "$Idrive"; print "$Zdrive"; }

My hash structure looks like this:

$VAR1 = { 'Application' => { 'Environment' => [ { 'Region' => [ { 'Zdrive' => ' +\\\\otherpath', 'Idrive' => ' +\\\\somepath', 'Name' => 'LN +' }, { 'Zdrive' => ' +\\\\otherpath', 'Idrive' => ' +\\\\somepath', 'Name' => 'NY +' } ], 'Name' => 'A' }, { 'Region' => [ { 'Zdrive' => ' +\\\\otherpath', 'Idrive' => ' +\\\\somepath', 'Name' => 'LN +' }, { 'Zdrive' => ' +otherpath', 'Idrive' => ' +\\\\somepath', 'Name' => 'NY +' } ], 'Name' => 'B' } ], 'Name' => 'App1' } };

20100908 Janitored by Corion: Added formatting, code tags, as per Writeup Formatting Tips

Replies are listed 'Best First'.
Re: Loop through hash structure
by planetscape (Chancellor) on Sep 06, 2010 at 11:18 UTC
Re: Loop through hash structure
by psini (Deacon) on Sep 06, 2010 at 10:36 UTC

    Always use strictures (use strict; use warnings;). First of all the line

    for my $record (@{my $VAR1->{Application}}) {

    shlould be

    for my $record (@{$VAR1->{Application}}) {

    for you don't want to redefine $VAR1 inside the loop...

    Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

      You have it backwards. That code came from his using strict. When told he was using a variable he never declared, He simply declared it instead of fixing the code to use the right variable name.

      Your solution is the code he had originally. While it undoes his second mistake, it leaves him with the first (not using the right variable).

        ++

        As an afterthought, I had suspected something like it, for $VAR1 is obviously the result of Dumper($something)

        Rule One: "Do not act incautiously when confronting a little bald wrinkly smiling man."

Re: Loop through hash structure
by roboticus (Chancellor) on Sep 06, 2010 at 10:32 UTC

    PerlScholar:

    You've already been told to do so, but you really should use code tags (<c> your code goes here</c>) to surround your code so that it gets rendered correctly in the browser.

    While I commend you for including some code and data, you're forgetting to describe what the problem is. Right now, I'd have to guess what the problem is, and I'd rather not go to the effort of figuring out which case is the most likely. I'll wait for another (more ambitious) monk to do so, or for you to provide a bit more detail.

    ...roboticus

Re: Loop through hash structure
by dasgar (Priest) on Sep 06, 2010 at 16:09 UTC

    First of all, you'll need to understand which components are hashes and which components are arrays in your data structure. It looks like you were able to apply some of the responses to your earlier question (XML:Simple Config). That's a good start.

    Secondly, you need to determine at which level of your data structure are you wanting to looping through. If that level is an array, it should be simple to iterate through that level with a for loop or a foreach loop. If that level is a hash, you can look at perlfaq4 for an example of how to iterate through a hash.

    However, I should point out that grantm gave you a reference to Perl documentation about complex data structures (perlreftut) in response to your earlier question (XML:Simple Config). You might find that information useful in dealing with your complex data structure.

    Also, I should point out that if you don't like the default structure that XML::Simple creates, there are some options in that module that you could use to modify how it will create the data structure.

      Hi dasgar,

      So I guess my structure is a hash of arrays and i'm having real trouble looping through this structure. Right now i'm only able to access the data by explicitly refering to the array index but would much rather be able to loop through the whole array.

Re: Loop through hash structure
by nvivek (Vicar) on Sep 07, 2010 at 05:28 UTC

    You can use the following code for getting the values of hash.

    for my $record ( $hash{'Application'}) { print "Environment:".$record->{'Environment'}[0]->{'Name'} ."\n"; print "Region:".$record->{'Environment'}[0]->{'Region'}[0]->{'Name'}." +\n"; print "Idrive:".$record->{'Environment'}[0]->{'Region'}[0]->{'Idrive'} +."\n"; print "LPad:".$record->{'Environment'}[0]->{'Region'}[0]->{'Zdrive'}." +\n"; }

    For your reference, I have used the index as 0.You can find the length of the array in the hash and you can loop through the array length.

      Hi Nvivek,

      This looks like what I need, however I would like to loop through all the indexes not just 0 or 1 is there a way to do that? Taking out the index number gives the error. Thanks!

      Pseudo-hashes are deprecated at Z:/My Documents/Workspace/Script.pl line 12.
      Argument "\x{51}\x{41}" isn't numeric in hash element at Z:/My Documents/Workspace/Script.pl line 12.
      Bad index while coercing array into hash at Z:/My Documents/Workspace/Script.pl line 12.

Log In?
Username:
Password:

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

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

    No recent polls found