CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Looping using XMLSimple

 



satnamx
New User

Jul 28, 2012, 11:33 AM

Post #1 of 4 (3576 views)
Looping using XMLSimple Can't Post

Hi All,

I have the below sample XML dump structure after reading it into XMLSimple:


Ive also uploaded an attachment of the actual XML example as the below is formatting screwed up!


$VAR1 = {
'Response' => [
{
'RTAG' => 'closure47',
'UUID' => 'Group000',
'Group' => [ {
'NAME' => 'null',
'GroupRef' => [
{
'GID' => 'Canada'
},
{
'GID' => 'United States'
}
],
'GID' => 'Group000',
'TYPE' => 'SELLFIRM',
'Property' => [
{
'VALUE' => 'SELLFIRM'
'NAME' => 'Level'
},
{
'VALUE' => 'Portfolio Type 1',
'NAME' => 'Portfolio Type'
},
............

............

...........



The below codes loops through above and returns the correct 'GID' within the 'Group' nodes:

my $i = 0;
my $j = 0;
foreach $e (@{ $data->{Response} }) {
for ($i = 0; $i < scalar (@{$e->{Group}}); $i++) { #Loop through the 'Group'
print "{Group}[$i]->{GID}: ", $e->{Group}[$i]->{GID}, "\n"; # get GID's

}
}

Im trying to use the above to output the 'GID's within the 'Groupref' nodes, something along the lines of this:

my $i = 0;
my $j = 0;
foreach $e (@{ $data->{Response} }) {
for ($i = 0; $i < scalar (@{$e->{Group}}); $i++) { #Loop through the 'Group'
print "{Group}[$i]->{GID}: ", $e->{Group}[$i]->{GID}, "\n"; # get GID's

for ($j = 0; $j < scalar (@{$e->{Group}->{GroupRef}}); $j++) { #Loop through the 'Group'
print "{Group}[$i]->{GroupRef}[$j]->{GID}: ", $e->{Group}[$i]->{GroupRef}[$j]->{GID}, "\n"; #'Canada'
}
}
}



but I get the below error:

{Group}[0]->{GID}: Group000
Can't use an undefined value as an ARRAY reference at testXMLSimple.pl line 51.

Clearly the portion of the for loop scalar (@{$e->{Group}->{GroupRef}}); ie trying to find the size of the array is causing the issue.

If i replaced the for loop with something like for ($j = 0; $j < 10; $j++) ... ths would work but obviously as a test, I cant harcode the upper limit to 10!

Ive also uploaded an attachment if the formatting of the XML is screwed up!

any ideas greatly appreciated?

Kind regards

Satnam



Any ideas on how to find the size of the array in the line


(This post was edited by satnamx on Jul 29, 2012, 3:47 AM)
Attachments: XMLSimple_Issue_ForLooping.txt (3.12 KB)


rovf
Veteran

Jul 31, 2012, 12:36 AM

Post #2 of 4 (3503 views)
Re: [satnamx] Looping using XMLSimple [In reply to] Can't Post

A few general remarks first:

(1) Make sure that you have "use strict; use warnings" active.

(2) Your error message indicates a line number, but you didn't indicate which was the offending line.

Having said this, I find the following contradiction in your program:

In the first loop, you use the expression


Code
@{$e->{Group}}


which implies that $e->{Group} is an array reference. In the second loop, you use the expression


Code
$e->{Group}->{GroupRef}


which implies that $e->{Group} is a hash reference. Since $e doesn't change in between, at least one of these two expressions must be incorrect.


satnamx
New User

Jul 31, 2012, 2:14 AM

Post #3 of 4 (3491 views)
Re: [rovf] Looping using XMLSimple [In reply to] Can't Post

Hi,

Thanks for the observations. in my full test script, I did actually use strict option and exact ouptput is below:

perl testXMLSimple.pl
-------------Section 1------------------------
RTAG: closure47
UUID: Group000
{Group}[0]->{NAME}: null
{Group}[0]->{GroupRef}[0]->{GID}: Canada
{Group}[0]->{GroupRef}[1]->{GID}: United States
{Group}[0]->{GID}: Group000
{Group}[0]->{TYPE}: SELLFIRM
{Group}[0]->{Property}[0]->{VALUE}: GRP01
{Group}[0]->{Property}[0]->{VALUE}: Level
{Group}[1]->{Property}[0]->{VALUE}: RGN01
----------------Section 2 ---------------------
{Group}[0]->{GID}: Group000
Can't use an undefined value as an ARRAY reference at testXMLSimple.pl line 51.


Line 51 in the test script is:

for ($j = 0; $j < scalar (@{$e->{Group}->{GroupRef}}); $j++) { #Loop through the 'Group'



My obseravtion is that as a test, Im outputting the following with hardcoded index values:

my $e;
foreach $e (@{$data->{Response}}) {
print "RTAG: ", $e->{RTAG}, "\n"; #closure47
print "UUID: ", $e->{UUID}, "\n";
print "{Group}[0]->{NAME}: ", $e->{Group}[0]->{NAME}, "\n"; #'null'
print "{Group}[0]->{GroupRef}[0]->{GID}: ", $e->{Group}[0]->{GroupRef}[0]->{GID}, "\n"; #'Canada'
print "{Group}[0]->{GroupRef}[1]->{GID}: ", $e->{Group}[0]->{GroupRef}[1]->{GID}, "\n"; #'United States'
print "{Group}[0]->{GID}: ", $e->{Group}[0]->{GID}, "\n"; #'Group000'
print "{Group}[0]->{TYPE}: ", $e->{Group}[0]->{TYPE}, "\n"; #'SELLFIRM'
print "{Group}[0]->{Property}[0]->{VALUE}: ", $e->{Group}[0]->{Property}[0]->{VALUE}, "\n"; #'SELLFIRM'
print "{Group}[0]->{Property}[0]->{VALUE}: ", $e->{Group}[0]->{Property}[0]->{NAME}, "\n"; #'Level'

print "{Group}[1]->{Property}[0]->{VALUE}: ", $e->{Group}[1]->{Property}[0]->{VALUE}, "\n"; #'RGN01'

}



and they all come back with the correct data elements particlulary the line:

print "{Group}[0]->{GroupRef}[0]->{GID}: ", $e->{Group}[0]->{GroupRef}[0]->{GID}, "\n"; #'Canada'


which is what Im trying to obtain via my looping logic. Im reading up on the "internmediate perl" chapter on references to try and get a better grip on the solution but so far still struggling.

Regards

Satnam


rovf
Veteran

Jul 31, 2012, 3:35 AM

Post #4 of 4 (3483 views)
Re: [satnamx] Looping using XMLSimple [In reply to] Can't Post

Exactly - and this is what I pointed out to you in my response: It is the fact that in $e->{Group}->{GroupRef} you treat $e->{Group} as a hashref.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives