Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Seeking some noob wisdom with XML::Simple

by downer (Monk)
on Feb 27, 2008 at 04:12 UTC ( [id://670476]=perlquestion: print w/replies, xml ) Need Help??

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

I am trying to parse the following kind of data structure:

$VAR1 = { 'ID' => 'booshoe37', 'ut_response' => [ { 'friend_list' => { 'friend' => [ { 'fri +end_count' => '97', 'use +r' => '4Article1', 'vid +eo_upload_count' => '0', 'fav +orite_count' => '26' }, { 'fri +end_count' => '29', 'use +r' => 'Annberly14', 'vid +eo_upload_count' => '6', 'fav +orite_count' => '255' }, { 'fri +end_count' => '97', 'use +r' => 'DamiansChannel', 'vid +eo_upload_count' => '3', 'fav +orite_count' => '21' }, { 'fri +end_count' => '3', 'use +r' => 'MaddieEgan', 'vid +eo_upload_count' => '1', 'fav +orite_count' => '0' }, ...
i'd like to be able to iterate through each of the entries in the 'friend' field, and handle each individually. the problem is, i have no idea how to do so. There is a difficult number of references here, and i have no idea what to foreach. can anyone help?

Replies are listed 'Best First'.
Re: Seeking some noob wisdom with XML::Simple
by bobf (Monsignor) on Feb 27, 2008 at 04:47 UTC

    foreach my $friendref ( @{ $VAR1->{ut_response}[0]{friend_list}{friend +} } ) { print "Hi $friendref->{user}\n"; }
    meets your requirements, but it will benefit you in the long run to learn about references. See perlreftut, perldsc, and perlref for starters. Our Tutorials page has a section entitled Data Types and Variables, which will also help.

    Update: Complex data structures can be tricky to wrap your head around at first. It might be easier to understand by breaking the solution into steps. Here is one way you could think about the goo in the foreach line, above:

    @{ # the array referenced by: $VAR1-> # dereference $VAR {ut_response} # then get the value of the hash key 'ut_respo +nse' [0] # then get the 0th element of that array {friend_list} # then get the value of the hash key 'friend_l +ist' {friend} # then get the value of the hash key 'friend' }

    Each one of those steps could be performed individually using temporary variables:

    my $ut_aref = $VAR1->{ut_response}; my $href = $ut_aref->[0]; my $flist_href = $href->{friend_list}; my $f_aref = $flist_href->{friend}; foreach my $friendref ( @{ $f_aref } ) ...

Re: Seeking some noob wisdom with XML::Simple
by hipowls (Curate) on Feb 27, 2008 at 04:37 UTC

    With deep structures like this you can use a temporary variable to access the bit you are interested in

    my $friends = $VAR1->{ut_response}[0]{friend_list}{friend}; foreach my $friend ( @{ $friends } ) { ... }

Log In?
Username:
Password:

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

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

      No recent polls found