in reply to Re: extracting values from hashes
in thread extracting values from hashes

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;

Replies are listed 'Best First'.
Re: Re: Re: extracting values from hashes
by broquaint (Abbot) on Mar 18, 2003 at 14:22 UTC
    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
        Just add a map before the grep like so
        my @data = map $hash{$_}, grep exists $hash{$_}, @array;

        HTH

        _________
        broquaint