RE: XML::Simple - how to iterate through records?
by Grant McLean other posts by this author
Jul 11 2001 12:43AM messages near this date
view in the new Beta List Site
New Benchmark LibXSLT against MSXML
|
RE: PerlSax to parse/search large (~350 MB) file
=====================================================================
Grant McLean | email: grantm@[...].nz | Lvl 6, BP House
The Web Limited | WWW: www.web.co.nz | 20 Customhouse Quay
Internet Solutions | Tel: +64 4 495 8250 | Box 1195, Wellington
Awesome service | Fax: +64 4 495 8259 | New Zealand
From: Phil Brodd [mailto:philip-brodd@[...].edu]
>
> I've been playing with XML::Simple, which seems quite nice.
> What I don't understand is how to refer to the entire array
> that is the value of the 'request' key below, or how to iterate
> through its elements.
>
> I am able to reference individual elements of the array with
> $hashref->{request}->[index], but how do I refer to the whole thing?
$hashref-> {request} is an array reference, so you could loop through
all the elements like this:
foreach my $request ( @{$hashref-> {request}} ) {
print $request-> {email}->[0], "\n";
}
similarly, you could extract the values to an array variable like this:
my @requests = @{$hashref-> {request}};
or you could get the index of the last one like this
my $last = $#{$hashref-> {request}}
I notice that you set forcearray to 1 in your code. That's good but
does introduce a level of inconvenience. A better way is to only
apply forcearray to those elements which can occur more than once.
For example, you can obviously have multiple <request> elements but
perhaps each request will have only one <user_id> . If you set
forcearray like this:
forcearray => [ 'request' ]
Then you can refer to the user_id for a particular request like this:
$hashref-> {request}->[$i]->{user_id}
instead of like this:
$hashref-> {request}->[$i]->{user_id}->[0]
Regards
Grant
_______________________________________________
Perl-XML mailing list
Perl-XML@[...].com
http://listserv.ActiveState.com/mailman/listinfo/perl-xml
|