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

Fellow Monks,

There's probably an easy answer to this, I have an xml file, for example:

<?xml version="1.0"?> <?leverage formatversion="1.0"?> <config servertype="sunhosts" reporttype="patches and levels" > <server name="atlas" osname="solaris" osversion="2.8"> <address>192.168.0.2</address> <address>192.168.0.22</address> <address>192.168.0.222</address> <patchlevel>Generic</patchlevel> <perlversion>5.6.1</perlversion> </server> </config>
I want to put the address values into a scalar, the equivalent of say:
$address="192.168.0.2 192.168.0.22 192.168.0.222";
I can pull these out one-at-a-time when I use:
$config->{server}{atlas}{address}->[0];
I thought just using
$config->{server}{atlas}{address}
would give me the result I was looking for, but it just returns a hash reference, I think. Is there an easy option to do this, or will I have to use a foreach loop?

Thanks
Jonathan

Replies are listed 'Best First'.
Re: XML::Simple question
by The Mad Hatter (Priest) on Mar 19, 2004 at 14:01 UTC

    This will work...

    my $address = join ' ', @{$config->{server}{atlas}{address}};

    For your information (since you seemed unsure), $config->{server}{atlas}{address} is an array ref, not a hash ref.

    Update Reworded last paragraph.

Re: XML::Simple question
by tinita (Parson) on Mar 19, 2004 at 14:31 UTC
    others have shown you how to get the array and how to display it in your desired format.
    now, just as a suggestion for future, whenever you wonder what exactly is in a data structure, use Data::Dumper or a dumper module of your choice. this will print out the structure nicely formatted.
    if you're additionally familiar with nested data structures in perl (from exercise and trying, or from reading perllol, perlref), then you won't have such problems any more. believe me =)
    enjoy
Re: XML::Simple question
by pbeckingham (Parson) on Mar 19, 2004 at 14:04 UTC

    If the following works for the first element:

    $config->{server}{atlas}{address}->[0]
    Then try
    my @array = @{$config->{server}{atlas}{address}};

      That puts the values in an array. What the OP seems to want is a space separated string of the values, or something like (using your example): my $address = "@array";