you're doing a lot of extra work in your code. my example below will read your set of locii into an array, create a set of molecules, and print the full locus record if a molecule is found in the first two tokens. records with less than six fields are skipped.

i skip the interim array (@locus_small in your code,) and use nested fors instead of grep, because i think it makes more sense. the really tricky bit is ( @{[]} = split /\s/ ) < 6, but i think my comments should help everyone understand what i'm doing.

the main loop is effectively six lines of code, which should be all you need. oh, and yes, i split on single space instead of tab in my example -- i'm too lazy to change the settings in my editor to spit out tabs instead of spaces ;-) enjoy!

#!/usr/bin/perl -w use strict; $|++; # create phony filehandle named OUTDATA (use STDOUT for debugging) *OUTDATA = *STDOUT; # create phony molecule list my @phony_molecule_list = qw( abc def abcd abc ); # create hash of molecules, to avoid duplicates my %molecules; # populate hash of molecules @molecules{@phony_molecule_list}++; # create list of locii, from DATA filehandle chomp( my @locii = <DATA> ); # for each molecule, sorted by longest word first for my $molecule (sort { length $b <=> length $a } keys %molecules) { LOCUS: for(@locii) { # skip if less than xxx tokens # i need to fake out split to get number of fields. usually # you can force list context by () = ..., but this doesn't # work with split. so, i force split to return its output # to an anonymous array (list context,) then evaluate the # anonymous array in scalar context to get number of element +s. next LOCUS if( ( @{[]} = split /\s/ ) < 6 ); # get first two fields (assumes at least three fields exist) my($test4match) = ( /(.+?\s.+?)\s/ ); # match a molecule (whole words only), and print the line if( $test4match =~ /\b\Q$molecule\E\b/ ) { print OUTDATA $_,$/; } } } __DATA__ abcd ghi 1 2 3 4 xyx yxy a b c abc xyx z y x w efg def 5 6 7 8 abc c o deg abc 9 0 1 abd abc x x

~Particle *accelerates*


In reply to Re: Re: Re: Parsing a tab delimited file by particle
in thread Parsing a tab delimited file by snowy

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.