Beefy Boxes and Bandwidth Generously Provided by pair Networks
Welcome to the Monastery

Accessing data in scalar with hashes and arrays

by Htbaa (Sexton)
on Nov 25, 2006 at 21:33 UTC ( #586051=perlquestion: print w/replies, xml ) Need Help??
Htbaa has asked for the wisdom of the Perl Monks concerning the following question:

Hi. I'm sorry if this question has popped up before but I've been searching and trying stuff and can't get it to work. I'm using XML::Simple to generate a XML file. Another script requests that file and uses XML::Simple to read it (XMLIn()). The result of XMLIn() is:
$VAR1 = [ [ { 'BERICHT' => 'test', 'IP' => '', 'NAAM' => 'test' }, { 'BERICHT' => 'test', 'IP' => '', 'NAAM' => 'Htbaa' } ], { 'rm' => 'start', 'dummy' => '' } ];
I've figured out how I can access the stuff like BERICHT, NAAM and IP. But I can't get 'rm' and 'dummy'. To get the contents of the array in this scalar I use
foreach (@{$ref}) { print "<b>". $_->{'NAAM'} . "</b><br>"; }
But how can I get 'rm' and 'dummy' ? I've tried a lot of different stuff but somehow can not get the data. All the examples on the web use a hash (instead $VAR1 it's %VAR1). But as long as I kind manipulate the result of XLMin() (or can I?) I have no idea on how to reach that data. Any help is more than welcome.

Replies are listed 'Best First'.
Re: Accessing data in scalar with hashes and arrays
by grep (Monsignor) on Nov 25, 2006 at 21:46 UTC
    This might help.
    my $var = [ # this signifies an anon array [ # Here is the first element of the top anon array - it i +s also an anon array { 'BERICHT' => 'test', 'IP' => '', 'NAAM' => 'test' }, { 'BERICHT' => 'test', 'IP' => '', 'NAAM' => 'Htbaa' } ], # end of 1st value of the top anon array { # Here is the 2nd value of the top anon array - It's an + anon hash 'rm' => 'start', 'dummy' => '' } ]; # So to access 'rm' you need the 2nd element of the top array (aka [1] +) # Deref that with $var->[1] # The 2nd element is an anon hash. That is where you need the hash key + 'rm' (aka {rm}) # Deref that with $var->[1]->{rm} print $var->[1]->{rm}; #prints 'start'

    XP matters not. Look at me. Judge me by my XP, do you?

      Oh my god was it that simple? I never tried that because, looking at the structure it looks like it's a scalar that contains an array and 2 loose elements (rm and dummy). But now I've read your comments on the structure I get it. Never thought of it like that. Thanks a lot :-). My Perl skills went down bigtime since I've been programming PHP for a few months again and have had nothing to do with Perl. I could've known it, darn :-P. In any case, I thank you a lot! Problem solved and I know now how to use these things in Perl.
Re: Accessing data in scalar with hashes and arrays
by NetWallah (Canon) on Nov 25, 2006 at 22:04 UTC
    If you are mixing Hash and array refs in the variable, you need to handle each separately:
    use strict; use warnings; my $ref = [ [ { 'BERICHT' => 'test', 'IP' => '', 'NAAM' => 't +est' }, { 'BERICHT' => 'test', 'IP' => '', 'NAAM' => 'H +tbaa' } ], { 'rm' => 'start', 'dummy' => '' } ]; foreach my $outer (@$ref) { if (ref($outer) eq "HASH"){ print "<b>$outer->{rm}</b></br>\n"; }else{ print "<b>". $_->{'NAAM'} . "</b><br>\n" for @$outer; } }
    <b>test</b><br> <b>Htbaa</b><br> <b>start</b></br>
    Oops - looks like grep++ beat me to showing you how.

         "A closed mouth gathers no feet." --Unknown

      Thanks anyway :-). The first array is passed to a HTML::Template object and the hash is used for extra information that's not being used by HTML::Template.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://586051]
Approved by chargrill
Front-paged by traveler
NodeReaper wonders through a maze of twisty little sayings, all alike

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2017-12-13 14:15 GMT
Find Nodes?
    Voting Booth?
    What programming language do you hate the most?

    Results (368 votes). Check out past polls.