On Jan 29, Jason Purdy said:

>my ( @filea, @fileb, @inAButNotInB, @inBButNotInA );

I don't see any hashes being used.  This feels like it's going to be very
inefficient.

>open ( FILEA, shift ) || die "Cannot open 1st file: $!\n";
>@filea = <FILEA>;
>close ( FILEA );
> 
>open ( FILEB, shift ) || die "Cannot open 2nd file: $!\n";
>@fileb = <FILEB>;
>close ( FILEB );
> 
>foreach my $object ( @filea ) {
>        chomp $object;
>        push @inAButNotInB, $object if ( !grep(/$object/, @fileb) );

And indeed it is.  Do not use grep(/$foo/, LIST) to determine if $foo is
in LIST, for a myriad of reasons.  First, you're using a regex, so if $foo
has any regex metacharacters in it, you'll get false negatives and false
positives.  Second, you're not anchoring the regex at all, so if LIST
contains "rabbit" and $foo is "bit", you'll get a false positive.  Third,
grep() goes through the ENTIRE list.  Imagine the consequences of

  if (grep $_ == 5, 1 .. 10_000) { ... }

You've just scanned 99,995 more elements than necessary!

The Perl FAQ suggests use of a hash.  See my response to this same
question.

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
** Look for "Regular Expressions in Perl" published by Manning, in 2002 **
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to