in reply to extracting non matched items from an array - the hard way!

Yer right, you want to use hashes:
# UNTESTED # after creating @words and @all for ( @all ) # iterate through @all, setting $_ to each in turn { my $four = ( split("\t", $_) )[3]; # split each line on \t, extract +4th field $seen{$four}++; # add to hash } # %seen hash has keys equal to the fourth field in # all the lines in @all @matched = grep { exists $seen{$_} } @words; @uniq = grep { not exists $seen{$_} } @words; # the preceding 2 lines could also have been: for ( @words ) { if ( exists $seen{$_} ) { push @matched, $_; } else { push @uniq, $_; } } # which has the advantage of iterating over @words only once.


--Bob Niederman, http://bob-n.com

Replies are listed 'Best First'.
Re: Re: extracting non matched items from an array - the hard way!
by jonnyfolk (Vicar) on Jul 13, 2003 at 03:45 UTC
    Many thanks, bob. The way you've set the hash on a single iteration of @all is just stunning. As I hoped there is much learnt (and to learn from this). I appreciate your explanation.