in reply to extracting values from hashes

my @array = qw/sarah john/; my %hash = ( sarah => 19, john => 25, emma => 22, ); print "$_: $hash{$_}\n" for grep exists $hash{$_}, @array; __output__ sarah: 19 john: 25
See. exists and grep for more info.
HTH

_________
broquaint

Replies are listed 'Best First'.
Re: Re: extracting values from hashes
by Anonymous Monk on Mar 18, 2003 at 14:13 UTC
    thanks for your help broquaint, this makes sense, but how would it be possible to push the information on the print line into an array as the following doesn't work;
    push @data, "$_: $hash{$_}\n" for grep exists $hash{$_}, @array;
      If you just want the array elements that exists as keys in the hash, a simple grep() should do the job.
      my @array = qw/sarah john/; my %hash = ( sarah => 19, john => 25, emma => 22, ); ## push() would be superfluous my @data = grep exists $hash{$_}, @array; print "data: @data\n"; __output__ data: sarah john

      HTH

      _________
      broquaint

        its really the values i wanted, not the keys. thanks though