http://www.perlmonks.org?node_id=1076362


in reply to Need Help with Maybe a Regex Issue

Under the assumption that the strings you are looking for are always the values of the /locus_tag, it is better to load the values you are searching for into a hash to use the exists function in addition to a regex. So you could loop over your second file, extract the value of the /locus_tag and if it exists in the hash, print the next line.

use strict; use warnings; my %hashOfVals; open(my $tmp, "<", "NC_001903.gbk.txt") || die "Could not open $!"; undef @hashOfVals{map {chomp; $_} <$tmp>}; close($tmp); open(my $tmpFile, "<", "cp26_diffexpr.txt") || die "Could not open $!" +; my @arrayFromCSV = <$tmpFile>; chomp @arrayFromCSV; close($tmpFile); for (0..$#arrayFromCSV-1) { print "$1: $arrayFromCSV[$_+1]\n" if $arrayFromCSV[$_] =~ /locus_tag="(.*)"/ and exists $hashOfV +als{$1}; }

Replies are listed 'Best First'.
Re^2: Need Help with Maybe a Regex Issue
by sharkbait (Initiate) on Feb 27, 2014 at 15:43 UTC

    Thanks for the reply! The code compiled and ran, but produced no output. I ran into the same problem with the C-style for loops (deleted now from the code, copied and pasted below). I wasn't completely sure if my problem was a regex issue, a problem in the implementation, or something simple I missed?

    for (my $j=0; $j<$#arrayFromCSV; $j++) { for (my $i=0; $i<$#wkArray; $i++) { if ("$arrayFromCSV[$j]" =~ /$wkArray[$i]/g) { print "$wkArray[$i+1]\n"; } } }