APM: Hashes and grep

Ian Ragsdale ian at SKYLIST.net
Mon Mar 17 12:23:49 CST 2003


On 3/17/03 12:07 PM, "Tom Bakken" <tom.bakken at tx.usda.gov> wrote:
> 
> Foreach (values %FIELDS) {
> Grep($ALL_FIELDS{$_}++,@$_);
> }
> 

The @$_ in the second argument to grep is trying to dereference an array
reference stored in $_.  If whatever is in $_ is not a reference, it will
complain.  Are you using "use strict;"?  I don't think it will complain
about that if you aren't.

You can get around it by making sure that $_ is an array reference:

Foreach (values %FIELDS) {
    if ( ref $_ eq 'ARRAY' ) {
        Grep($ALL_FIELDS{$_}++,@$_);
    }
}


> And:
> 
> grep ($fields{$_}++, @fields);
> (@hidden_fields = grep(!$fields{$_}, keys %ALL_FIELDS);
> 

The above code looks ok to me, unless you are running under 'use strict' and
have forgotten to declare %fields.  The first grep will stick a value in
%fields for each element of @fields, and then the second grep will return an
array with every key of %ALL_FIELDS that does not appear in %fields.

HTH,
Ian




More information about the Austin mailing list