Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Need Help with Maybe a Regex Issue

by hdb (Prior)
on Feb 27, 2014 at 07:21 UTC ( #1076362=note: print w/ replies, xml ) Need Help??


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}; }


Comment on Re: Need Help with Maybe a Regex Issue
Select or Download Code
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"; } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076362]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (16)
As of 2015-07-29 14:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (263 votes), past polls