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

A call for help from the wise monks.I have made an assimilation of the subroutine I am tring to write as little program below, along with its output. I have tried many ways to get into the array embeded in the data structure but am probably missing something very simple.
use strict; use XML::Simple; use Data::Dumper; my ($user, $service) = ('Scarborough','dbjobs'); my $user_hash; #the real input comes from a file and is very much longer $user_hash = XMLin("<SERVICES> <SERVICE name='dbjobs'> <USER>Scarborough</USER> <USER>Whitby</USER> </SERVICE> <SERVICE name='reports'> <USER>Scarborough</USER> </SERVICE> </SERVICES>"); print Dumper($user_hash); #only here for testing if ($$user_hash{SERVICE}->{$service}){ print "$service exists\n"; #Here the problem begins #Why is @users getting a ref to the array my @users = @$user_hash{SERVICE}->{$service}->{USER}; print Dumper(@users); #have tried: foreach my $this_service (@$user_hash{SERVICE}->{ +$service}->{USER}) foreach my $this_user (@users){ print "\n".$this_user." ".$user."\n"; } } __END__ OUTPUT FROM PROGRAM $VAR1 = { 'SERVICE' => { 'dbjobs' => { 'USER' => [ 'Scarborough', 'Whitby' ] }, 'reports' => { 'USER' => 'Scarborough' } } }; dbjobs exists $VAR1 = [ 'Scarborough', 'Whitby' ]; ARRAY(0x1a79ea8) Scarborough
Thank you for your consideration.

Replies are listed 'Best First'.
Re: Derefferencing the out put from XMLin()
by Plankton (Vicar) on Jun 08, 2004 at 16:14 UTC
    Will this solver your problem?
    Change this ...
    my @users = @$user_hash{SERVICE}->{$service}->{USER};
    ... to ...
    my $users = @$user_hash{SERVICE}->{$service}->{USER};
    ... and this line ...
    foreach my $this_user (@users){
    ... to this ...
    foreach my $this_user (@{$users}){
    ... Hope that helps.

    Plankton: 1% Evil, 99% Hot Gas.
Re: Derefferencing the out put from XMLin()
by antirice (Priest) on Jun 08, 2004 at 16:29 UTC

    Hmmm, fairly odd behavior. I think changing two lines will make it do what you expect:

    # Was if ($$user_hash{SERVICE}->{$service}){ # Equivalent to if (${$user_hash}{SERVICE}->{$service}){ if (exists $user_hash->{SERVICE}{$service}) {
    # Was my @users = @$user_hash{SERVICE}->{$service}->{USER}; my @users = @{ $user_hash->{SERVICE}{$service}{USER} };

    Note that the proper way to access a hashref is $hash->{blah}.

    antirice    
    The first rule of Perl club is - use Perl
    The
    ith rule of Perl club is - follow rule i - 1 for i > 1