http://qs1969.pair.com?node_id=431442


in reply to Re: Frontier::Client mystery result string hash
in thread Frontier::Client mystery result string hash

Yup, you got it. I had been unable to create a matching data structure prior to your hint. This works:
print "Print city: $result->[0]{'city'}\n";
But I decided the following looked "cleaner". It makes me cringe, but here it is:
my %hash = %{$result->[0]}; # copy result to new hash ... print "Print city: $hash{'city'}\n";
I still can't quite figure out why Frontier::Client put this result in such a pointlessly nested data structure...

Replies are listed 'Best First'.
Re^2: Frontier::Client mystery result string hash
by jbrugger (Parson) on Feb 16, 2005 at 05:44 UTC
    Why would this be cleaner? You've got a reference to an array, containing a hash, meaning it only exists 1 time in your memory.
    Now you're dereferencing it into another hash, resulting in being twice in the memory. Do this with larger hashes over and over, and the memory usage becomes bigger and bigger.
    Print city: $result->[0]{'city'}\n"; is a verry legitimate way to write the results, i'd like the iterative way even more, in $result->[1] may be another hash, so i'd use ikegami's approach:
    foreach $record ( @{$result} ) { foreach $field (keys %$record) { printf("%s: %s\n", $field, $record->{$field}, ); } print("\n"); }
Re^2: Frontier::Client mystery result string hash
by ikegami (Patriarch) on Feb 16, 2005 at 06:46 UTC

    This doesn't create a copy, just an alias:

    local %hash; *hash = $result->[0]; ... print "Print city: $hash{'city'}\n";
    I still can't quite figure out why Frontier::Client put this result in such a pointlessly nested data structure...

    Could it possibly return more than one result? Or maybe it's just extending a generic module that returns the data in that fashion.

      Tricky! Even the camel book harly touches on the * operator, relegating it to 'supported in C' in the index... I settled on the decidedly untricky:
      $geocode = $result->[0]; print "Match: lat=$geocode->{'lat'} long=$geocode->{'long'}\n";
        It's not a operator. It's a variable-type sigil like $, @, % and &. It refers to an entry in the symbol table. While manipulating the symbol table (as I did) is an advanced topic, it is well documented.
Re^2: Frontier::Client mystery result string hash
by dragonchild (Archbishop) on Feb 16, 2005 at 13:22 UTC
    I still can't quite figure out why Frontier::Client put this result in such a pointlessly nested data structure...

    Because it's not pointless. The XML-RPC protocol allows for arrays of hashes. This means that the outer array is useful. It's just not useful in your situation.

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.