CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Intermediate:
Reading in a tab delim file as an array of arrays but issues with boolean

 



amindlessbrain
New User

Dec 13, 2010, 1:19 AM

Post #1 of 4 (1132 views)
Reading in a tab delim file as an array of arrays but issues with boolean Can't Post

Hello all,

I have a perl program I am trying to use to parse an two tab delim files and find overlap between one of the categories.

They both look something like:

Symbol\tGeneID\GeneDesc\tGenBank
x\ty\tz\ta

So I have read in each file as an array of arrays, with each line being its own array.


Code
while( <$in1>){ 
my $line = "$_";
#I'm sure there's a better way to get rid of the header line...
if($count==0){
$count++;
}
else{
chomp($line);
@tmp = split(/\t/, $line);
push(@fields1, [@tmp]);
}
}


Same thing for $in2, except that its @fields2.

Now I want to step through and find matches.


Code
for (my $x = 0; $x <= $#fields2; $x++) { 
$count++;
for (my $a = 0; $a <= $#fields2; $a++) {
if ($fields1[$x][4] == $fields2[$a][4]){
print $out "$fields2[$a][4]\n";
}
}
}


The trouble is that it decided that every element of every array is true!

As a check I did:


Code
print "$fields1[0][4]\n$fields2[0][4]\n\n"; 

my $var1 = $fields1[0][4];
my $var2 = $fields2[0][4];

if(($fields1[0][4]) == ($fields2[0][4])){
print "fields True!\n";
}
else{
print "fields False!\n";
}

if($var1 == $var2){
print "var True!\n\n";
}
else{
print "var False!\n\n";
}


And as output I get:


Code
BG434174 
AB028973

fields True!
var True!


Any idea why it would do this? I have this same setup for another parsing/overlap program with slightly different parameters, and it works. I don't get why it would do this.

Any insight would be very appreciated!

~mb


amindlessbrain
New User

Dec 13, 2010, 4:20 AM

Post #2 of 4 (1129 views)
Re: [amindlessbrain] Reading in a tab delim file as an array of arrays but issues with boolean [In reply to] Can't Post

I figured it out, but for anyone who wants to know you can't do a boolean "==" with this kind of data. The array of array thing seems to get a bit buggy.

What you need to do is use a regular expression, but ONLY if you are comparing text. If your input is numbers, then you can stick to the boolean.

So:

Code
my $var1 = $fields1[$x][$y]; 
my $var2 = $fields2[$a][$b];
if($var1 =~ m/$var2/){
print "True!";}
else{print "False!";}


Maybe that will help someone. =)


Zhris
Enthusiast

Dec 13, 2010, 4:40 AM

Post #3 of 4 (1127 views)
Re: [amindlessbrain] Reading in a tab delim file as an array of arrays but issues with boolean [In reply to] Can't Post

Hi,

In Perl, when comparing numbers you would use i.e. "==", but when comparing strings you would use i.e. "eq". Therefore I believe "eq" would have worked in your case.

The regular expression you are using won't neccessarily find an exact match. For example:


Code
my $var1 = "mystring"; 
my $var2 = "string";
if ($var1 =~ m/$var2/) {print "True!";}
else {print "False!";}


Although var1 and var2 aren't the same, the regular expression will return true because var2 is contained within var1. A better regular expression would be:


Code
m/^\Q$var2\E$/;


^, $ signal the beginning and the end of the "row of data". \Q and \E are simply there so that the $ in $var2 isn't regarded as a special character.

Chris


(This post was edited by Zhris on Dec 13, 2010, 4:44 AM)


amindlessbrain
New User

Dec 13, 2010, 7:54 AM

Post #4 of 4 (1115 views)
Re: [Zhris] Reading in a tab delim file as an array of arrays but issues with boolean [In reply to] Can't Post

Great! That is good to know. I'll try it out both ways, with the eq and the other regular expression you gave. It did work with just m/" "/, but I always like to make the code the best it can be. =)

Thanks so much!

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives