Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

Re: Need Help with Maybe a Regex Issue

by hdb (Monsignor)
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}; }

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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1076362]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2018-06-18 18:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.