SPUG: Re: multilevel hash lookups

Yitzchak Scott-Thoennes sthoenna at efn.org
Fri Aug 16 00:11:44 CDT 2002


On Thu, 15 Aug 2002 14:25:46 -0700, eric.brose at attws.com wrote:
>@HBSource=("->{vtComponent}->{deployName}","->{vtComponent}->{vtFlow}->{vtPr
>operty}->{channel}->{value}","->{vtComponent}->{vtFlow}->{vtProperty}->{host
>name}->{value}","->{vtComponent}->{vtFlow}->{vtProperty}->{QManager}->{value
>}","->{vtComponent}->{vtFlow}->{vtProperty}->{transactionQueue}->{value}","-
>>{vtComponent}->{vtFlow}->{vtProperty}->{QName}->{value}"); 
>
>		foreach (@HBSource){
>			$value1 = $hash1."$_";
>			$value2 = $hash2."$_";
>			$value3 = $hash3."$_";

Sounds like your data structure doesn't quite match what you want to
do with it.  Perhaps some OO encapsulation is in order?  Nevertheless,
to do it the way you are trying, you need to reinvoke the parser at
run-time.  The way to do this is with eval EXPR:

foreach (@HBSource) {
    eval("\$value1 = \$hash1$_;
          \$value2 = \$hash2$_;
          \$value3 = \$hash3$_;
          1") || die "bad code in \@HBSource ($_): $@";
}

Replace the eval with print to see the code it is using.

Alternatively, something like:

sub deep_lookup {
    my ($val, $keys) = @_;
    for my $key (@$keys) {
        $val = $val->{$key}
    }
    return $val
}

@HBSource = (["vtComponent", "deployName"], ["vtComponent", "vtFlow"], ... );
foreach $src (@HBSource) {
   $value1 = deep_lookup($hash1, $src);
   ...
}

 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
      Subscriptions; Email to majordomo at pm.org:  ACTION  LIST  EMAIL
  Replace ACTION by subscribe or unsubscribe, EMAIL by your Email-address
 For daily traffic, use spug-list for LIST ;  for weekly, spug-list-digest
     Seattle Perl Users Group (SPUG) Home Page: http://seattleperl.org




More information about the spug-list mailing list