in reply to RegExpression fails to work

To answer your primary question, the /^$species_filter/ match is failing because the $species_filter does not come at the beginning of the string. Remove the caret to match correctly.

for the string:

'AF035737 Homo sapiens general transcription factor 2-I (GTF2I) mRNA, +complete.' /Homo sapiens/ will match /^Homo sapiens/ will not

However there are many other things wrong with this routine. Here's a few comments.


sub filter_by_Species() # Don't use prototypes unless you NEED them. And even then, you probab +ly don't. open( NEWALIGN, "<blast_files/$newBlastFile") || die "$!"; #use lexical filehandles and three argument opens to prevent # obscure bugs. The || is very high precedent. Not wrong # really, but unecessarily powerful. #read results file one line at a time my @resultLine = <NEWALIGN>; # Arggh. Read entire file into an array at once. my @subjects = []; # reference to empty array # nope. Array whose first element is a reference to an empty array #so I produce a hash with key = >gi line of report and value = all lin +es after until next >gi # I saw no "greater than" in the example data... for (my $i = 0 ; $i<scalar @resultLine; $i++) # C style loops. Avoid unless necessary. They work ok but # clutter up the script and make it harder figure out what # you are doing. if ($resultLine[$i] =~ /^>/) # again, no "greater than" in the example data... { $current_subject = $resultLine[ $i ]; chomp ($current_subject); push (@subjects, $current_subject); $alignment->{$current_subject} = ""; $alignment->{$current_subject}.$resultLine[ $i ]; } # you never use the %$alignment hash anywhere so why bother? my @elements; my $boolean; # declare variables in the smallest scope possible foreach my $z (@subjects) { chomp $z; # already chomped in previous loop show_Output($boolean, $z, @elements); # boolean ALWAYS equal 0. Why not just pass 0?

If I were to write this getting the same functionality it would look something like:


sub filter_by_Species { print 'New Blast File Opened<BR>'; open(my $newalign, '<', "blast_files/$newBlastFile") or die $!; print 'Filtering Data........Please wait<P>'; my @subjects; while <$newalign> { push @subjects, $_ if (/^>gi/) } print '<TABLE BORDER=2>'; print '<th align=center><B>Status</B></th>'; print '<th align=center><B>Information on new hits</B></th>'; for (@subjects) { chomp; my @elements = split '\|'; if($elements[-1] =~ /$species_filter/) { print "<TR><TD>Match to $species_filter"; show_Output(0, $_, @elements); } } print'</TABLE><BR><BR>'; }

Also, since your show_Output used blank prototypes, it never recieves the passed values. Again, don't use prototypes unless you NEED to. Never mind that last bit, that is not true.

Update: fixed a few typos.

Replies are listed 'Best First'.
Re: RegExpression fails to work
by MonkPaul (Friar) on Jun 14, 2005 at 15:04 UTC
    Thanks a lot for your help.

    The symbol " > " was not a greater than symbol though, it is the start of the record in the file: >gi|2827179|gb|AF035737.1|AF035737........etc I was not aware that the prototypes were also causing a problem. Will have to sort that out. thanks.

    MonkPaul.