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

I am querying an appliance and the response received is an array of hash references. I can read these just fine. Where I am having all my trouble is when I try to add fuctionality to allow me to query multiple (let's just start with 2) boxes at 1 time. I have tried setting up an array where each index is the array of hrefs and I've tried setting up an array with array references. What I have below is my latest attempt before asking for help. A better and shorter way to restate my question, and what I am having difficulty understanding from documentation is how I can take the list returned by the sub and put it a reference to it into the array @hostresults into index 0. Thank you, Frank
#!/usr/bin/perl use strict; use warnings; use SOAP::Lite; my @Data = @ARGV; # process box info my %host1 = ('ip'=>$Data[1],'user'=>$Data[2],'password'=>$Data[3],'ver +sion'=>$Data[4]); my %host2 = ('ip'=>$Data[5],'user'=>$Data[6],'password'=>$Data[7],'ver +sion'=>$Data[8]); my (@results,@results1,@results2); my (%hresults1,%hresults2); my @hostdata = (\%host1,\%host2); my @hostresults ; my ($ip,$user,$password,$version); for (my $i=0;$i<=1;$i++){ $ip = ${hostdata[$i]}{ip}; $user = ${hostdata[$i]}{user}; $password = ${hostdata[$i]}{password}; $version = ${hostdata[$i]}{version}; if ($version eq "ggg"){ @results = &get_list(); $hostresults[0] = &get_list(); print "RESULTS = @results\n"; print "--------------------\n"; print "HOSTRESULTS = ${$hostresults[0]}\n"; # <<< LIne 28
scratch.pl vstestlist.txt 172.24.17.41 admin adminggg 172.24.23.32 adm +in admin ddd RESULTS = GlobalLB::VirtualServerDefinition=HASH(0x3d7b504) GlobalLB:: +VirtualSer verDefinition=HASH(0x3d7afb4) GlobalLB::VirtualServerDefinition=HASH(0 +x3d793f4) GlobalLB::VirtualServerDefinition=HASH(0x3d79414) GlobalLB::VirtualSer +verDefinit ion=HASH(0x3d78f94) GlobalLB::VirtualServerDefinition=HASH(0x3d78d3c) +GlobalLB:: VirtualServerDefinition=HASH(0x3d78acc) GlobalLB::VirtualServerDefinit +ion=HASH(0 x3d788dc) GlobalLB::VirtualServerDefinition=HASH(0x3d786ac) GlobalLB:: +VirtualSer verDefinition=HASH(0x3d7847c) GlobalLB::VirtualServerDefinition=HASH(0 +x3d7820c) GlobalLB::VirtualServerDefinition=HASH(0x3d7801c) GlobalLB::VirtualSer +verDefinit ion=HASH(0x3d77dc4) GlobalLB::VirtualServerDefinition=HASH(0x3d77b94) -------------------- Can't use string ("14") as a SCALAR ref while "strict refs" in use at +scratch.pl line 28.

Replies are listed 'Best First'.
Re: using / accessing nested hash/array references
by state-o-dis-array (Hermit) on Jan 18, 2011 at 23:30 UTC
    These:
    @results = &get_list(); $hostresults[0] = &get_list();
    are not equivalent. You have encountered a context issue, the first line above is list context, the second is scalar, in other words, $hostresults[0] is not an array as I think you are expecting. I think what you would want to do is change the second line to something like:
    $hostresults[$i] = \@results;
    Then $hostresults[0] is a reference to your first data set.

    You might want to look at perldsc and perlref

      I changed the line to
      $hostresults[0] = \@{&get_list()};
      and I am still receiving the scalar error. I have experimented with initializing like
      my @hostresults = (\@result1,\@result2); #or my @hostresults = (@result1,@result2);
      but I still received errors. My attempts would have been with the same context though. The right side sub returns the array of hash references which is why I left in the line above in my original example. My hope was to show what was happening outside of my context or syntax errors. Thank you
        Sorry, I was kind of hoping that having changed how you are assigning to $hostresults[0] that you might take that as a clue to look at how you are accessing it. Anonyrnous Monk provides a valuable suggestion in referring you to Data::Dumper. Also, have you taken time to check out the links I provided? I think that they will be helpful to you.
Re: using / accessing nested hash/array references
by Anonyrnous Monk (Hermit) on Jan 18, 2011 at 23:40 UTC
    Can't use string ("14") as a SCALAR ref ...

    You're getting the error because $hostresults[0] apparently evaluates to 14 (what get_list() returns in scalar context), which you then try to dereference as a scalar by putting ${} around it.

    It's not really clear to me what you want to be printed in line 28, so it's difficult to advise how to achieve it.  In case you want to print some complex data structure, I'd recommend Data::Dumper  (e.g. print "RESULTS = ", Dumper(\@results); ).

      Thank you for the reply but the output was there only to provide output to show people the nature of the data I was working with. This and the other responses allowed me to recognize why I'm seeing the scalar value of the array. I am still wondering if or how I could manage this though and that is what I am still fighting. I want to take what is returned by the subroutine, a list of hashes, and I want to place that into a list. The goal being that the list of hashes for box1 is in index0, list of hashes for box2 is in index1, and so on.