in reply to Re: Parsing a tab delimited file
in thread Parsing a tab delimited file

there are a few pitfalls to using the code posted above. i'd like to take a minute to explain some of them to everyone, in no particular order.

all in all, your code will work with a few modifications. i find it a little obfuscated, though. here it is, with the changes i've suggested.

#!/usr/bin/perl -w use strict; open( MOL, "molecules" ) or die "ack! - $!"; push my @molecules, quotemeta chomp while <MOL>; close MOL or warn "ack - $!"; my $bigRegex; $bigRegex = join '|', map { "\b$_\b" } sort { length $b <=> length $a } @molecules; open( LOCUS, "locus" ) or die "ack! - $!"; my @locus = <LOCUS>; close LOCUS or die "ack! - $!"; for(@locus) { print if( scalar( split /\t/ ) >= 6 && /^([^\t]*\t)?($bigRegex)/ ); }
by the way, i like your use of ?() in the regex. i recommend readers investigate this powerful construct by reading about it in perlre.

~Particle *accelerates*

Replies are listed 'Best First'.
Re: Re^2: Parsing a tab delimited file
by graff (Chancellor) on May 13, 2002 at 20:59 UTC
    Thanks -- those are mostly good points... except there's a problem in the second item:
    I'd replace
    @molecules = <MOL>; map { chomp; $_ = quotemeta; } @molecules;
    with
    push my @molecules, quotemeta chomp while <MOL>;
    The problem with this replacement is that chomp simply modifies its arg ($_ in this case), but does not return the modified arg -- something other than the chomp'ed string gets returned to quotemeta, and pushed onto @molecules.

    I just recently tried this sort of approach in an attempt to shorten a one-liner, and didn't get what you expect:

    push @m, quotemeta chomp while <DATA>; print join("|",@m),$/; __DATA__ AAA BBB &*(
    Yields:
    1|1|1

    Maybe "map" in a void context isn't sexy, but it does work. (I agree that grep in a void context would be silly.)

      you're right! i should have tested before posting. i still think map in void context is a bad idea, so i came up with this: my @molecules = map { chomp; quotemeta } <DATA>; ...but is it sexy enough ;-)

      ~Particle *accelerates*