in reply to SOAP::Lite and hash of hashes

In the foreach you are converting the hash into an array and loop through all the keys and values. So $r is a key in the first iteration, then a value, then a key. $r->result makes no sense either, since 'result' is the name of the hash and should be before the key

foreach my $r (keys %$result){ print $result->{$r}->{Person}->{commonname}; }

$result is a pointer to a hash and $r is now a key of it (note the keys function)

Replies are listed 'Best First'.
Re^2: SOAP::Lite and hash of hashes
by joec_ (Scribe) on Feb 12, 2009 at 14:45 UTC
    Thanks for the reply - that code gives me:Cant coerce array into hash. What does that mean? Also - as far as i know with soap, $result->result is a method called on the $result variable

    Thanks

    -----

    Eschew obfuscation, espouse eludication!

      Ah sorry, that that was an object method didn't cross my mind. I probably can't really help you as I don't know Soap::Lite and I didn't find the search method in its documentation. But one thing is certain, your foreach is not right. It might be one of these:

      foreach my $r (keys %$result){ print $r->result->{Person}->{commonname}; } foreach my $r ($result->result){ print $r->{Person}->{commonname}; } foreach my $r (@$result){ print $r->result->{Person}->{commonname}; }

      If not, you might use Data::Dumper to check out the data structure of $result. Or look for an example in the documentation of the search method

      The error message 'Can't coerce...' means there is an array somewhere and we access it as a hash. Data::Dumper is your friend here.

      UPDATED

        Hi, thanks for responding.
        foreach my $r (keys %$result){ print $r->result->{Person}->{commonname}; }
        gives Can't locate object method "result" via package "_content" (perhaps you forgot to load "_content"?) at ./searchPeople.pl
        foreach my $r ($result->result){ print $r->{Person}->{commonname}; }
        works for a unique item i.e. login id but gives the pseudo hash deprecated error and bad index error as above

        foreach my $r (@$result){ print $r->result->{Person}->{commonname}; }
        Gives 'not an ARRAY reference'.

        This is output from data dumper when the search method is given 'shaw', ive truncated a load of other data and other people for demo purposes:

        $VAR1 = { 'Person' => [ { 'commonname' => 'person 1', 'location' => 'location 1', 'telephonenumber' => '34567688', 'surname' => 'Shaw' }, { 'commonname' => 'person 2', 'location' => 'location 2', 'telephonenumber' => '1234576', 'surname' => 'Birkinshaw' }
        Looking at this then, is Person an array and not a hash? How would i get at each element of the person array if it is?

        The search method isnt part of the SOAP::Lite docs - its a web method from my webservice.

        Thanks

        Joe

        UPDATE I can get at the second person using

        print $result->result->{Person}[1]->{commonname};

        Now all i need is a loop...thanks

        -----

        Eschew obfuscation, espouse eludication!