perl - HASH element from an object's return in a single string -
i have module module.pm function gethash()
returns, guessed, hash of data)). know how element form hash, using standard model:
use module; $m = new module; %hash = $m->gethash(); print $hash{needed_element's_key};
everything ok. if need write 2 last strings in single string without initializing new hash (and occupying memory whole hash, using 1 element)?
is possible somehow? like, say, $m->gethash()->{needed_element's_key};
of course, given example not work. suggestions? thanks!
it's impossible return hash. subs can return list of scalars. assigning list of scalars existing hash my %hash
, getting element of hash.
you disguise fact creating new hash using anonymous hash:
my $val = { $m->getkeyvalpairs() }->{$key};
alternatively, custom filter more efficient:
sub find_val { $target_key = shift; while (@_) { $key = shift; $val = shift; return $val if $key eq $target_key; } return undef; } $val = find_val $key, $m->getkeyvalpairs();
but best option have method return reference hash instead. then, wouldn't have perform wasteful activity of creating hash. you'd use following:
$m->gethashref()->{$key}
Comments
Post a Comment