467,179 Members | 1,277 Online
Bytes | Developer Community
Ask Question

Home New Posts Topics Members FAQ

Post your question to a community of 467,179 developers. It's quick & easy.

doubt in if statement

I have done few basic perl programming and after spending some time in debugging, I thought of getting some help from this forum. Please let me know whether my program has gone wrong.

Basically I have a text file, each line consists of 8 columns which are tab separated. I am trying to check columns at 4 and 8 and trying to print the whole line. I have done small scripts like this before, may be something wrong today. No idea where I went wrong.
Here comes my code

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl
  2. while(<>) {
  3.      chomp $_;
  4.      my(@v) = split(/\t,$_);
  5.      print"Column4: $v[3] \t Column8: $v[7] \n";
  6.      if($v[3] eq 'F' && $v[7] eq 'R') {
  7.       print "$v[3] \t $v[7] \n";
  8.       }
  9. }
  10.  
  11.  
print"Column4: $v[3] \t Column8: $v[7] \n"; basically print correctly as below:

print"Column4:F Column8:R \n";

I am wondering, if the condition is true, why the control of the program does not go inside the if statement and prints print "$v[3] \t $v[7] \n"; statement line. I am really puzzled with the reason not going inside if statement when I know that for sure that this condition is true. Please let me know
Nov 8 '08 #1
  • viewed: 1546
Share:
9 Replies
KevinADC
Expert 2GB
The code you posted should not even compile let alone run. If you are not getting an error when you try that code then maybe that is not the code you are actually using. This line:

Expand|Select|Wrap|Line Numbers
  1.      my(@v) = split(/\t,$_);
is missing the ending delimiter in the regular expression after \t: /


fix that and retry your code. It is highly recommended to always run your perl programs with strict and warnings turned on:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Nov 8 '08 #2
The code you posted should not even compile let alone run. If you are not getting an error when you try that code then maybe that is not the code you are actually using. This line:

Expand|Select|Wrap|Line Numbers
  1.      my(@v) = split(/\t,$_);
is missing the ending delimiter in the regular expression after \t: /


fix that and retry your code. It is highly recommended to always run your perl programs with strict and warnings turned on:

Expand|Select|Wrap|Line Numbers
  1. use strict;
  2. use warnings;
Thanks Kevin. My delimiter is only "\t" in the text file. Whta I am trying to say is when I print those two columns in the print statement, the values are either "F or R", but the control never goes inside the if statement. I am wondering why. I have used Column: just for printing purpose.
Nov 9 '08 #3
KevinADC
Expert 2GB
You probably mean to use || (or) instead of && (and) :

Expand|Select|Wrap|Line Numbers
  1. if($v[3] eq 'F' || $v[7] eq 'R') {
the way you have it (&&) they both must return true for the "if" condition to be true and the block evaluated.

The regexp still needs the terminating slash / after \t:

Expand|Select|Wrap|Line Numbers
  1. my(@v) = split(/\t/,$_);
Nov 9 '08 #4
You probably mean to use || (or) instead of && (and) :

Expand|Select|Wrap|Line Numbers
  1. if($v[3] eq 'F' || $v[7] eq 'R') {
the way you have it (&&) they both must return true for the "if" condition to be true and the block evaluated.

The regexp still needs the terminating slash / after \t:

Expand|Select|Wrap|Line Numbers
  1. my(@v) = split(/\t/,$_);
Yes Kevin. Thanks for your response. My original script consists of
Expand|Select|Wrap|Line Numbers
  1. my(@v) = split(/\t/,$_);
/\t/ as the delimiter as mentioned by you and some typos while writing in this program That is the reason why the following line above if statemnt worked and it printed the columns as either F or R

print"Column4: $v[3] \t Column8: $v[7] \n";

Then I changed my if statement as below and it works fine now.
Expand|Select|Wrap|Line Numbers
  1. if(($v[3] =~ m/F/) && ($v[7] =~ m/R/)) {
  2. #process
  3. }
  4.  
This clearly shows something wrong in constructing my data file. But definitely $v[3] and $v[7] consisits of either a single capital character R or F. But I am still wondering why direct comparison like
Expand|Select|Wrap|Line Numbers
  1. if($v[3] eq 'F' && $v[7] eq 'R') {
does not work as that column is a single character as F or R. PLEASE CLARIFY my doubt.
Thanks.
Nov 9 '08 #5
KevinADC
Expert 2GB
I can only guess without seeing your data. There could be a newline or other non printable ASCII character in the array elements you are checking that fail to match when you use "eq" but will match if you use a regular expression because that is a substring match.

Instead of /\t/ try splitting the lines with /\s+/.
Nov 9 '08 #6
I have problems in downstream data analysis. Can any one tell how to remove some extra ascii characters in the data file?
Nov 10 '08 #7
nithinpes
Expert 256MB
I have problems in downstream data analysis. Can any one tell how to remove some extra ascii characters in the data file?
You can make use of substitution operator (s///).
Nov 10 '08 #8
Hi, Thanks for your response. Sorry I am bit confused. I tried as below to remove the white space but I need the tab separated columns in order to parse the data.

Expand|Select|Wrap|Line Numbers
  1. #!usr/bin/perl 
  2. while(<>) { 
  3.      chomp $_; 
  4.      s/\s+//g; #removes the blank space
  5.      my(@v) = split(/\t/,$_); 
  6.      print"Column4: $v[3] \t Column8: $v[7] \n"; 
  7.      if($v[3] eq 'F' && $v[7] eq 'R') { 
  8.       #process somehing
  9.       } 
  10.  
The above code removes all th white space and $_ line contains one full string which are not \t tab separated in order for me to go inside if statement. Hence control doesnot enter the if statement as the string $_ is not tab separated. Where as if I split as \s+\ as Kevin suggested, control enters the IF statement but I want to remove only \s+ and don't want to disturb the tab separated columns in order for me to carry on further analysis.


Expand|Select|Wrap|Line Numbers
  1.  
  2. #!usr/bin/perl 
  3. while(<>) { 
  4.      chomp $_; 
  5.          my(@v) = split(/\s+/,$_); 
  6.      print"Column4: $v[3] \t Column8: $v[7] \n"; 
  7.      if($v[3] eq 'F' && $v[7] eq 'R') { 
  8.       #process something
  9.       } 
  10.  
  11.  
Please let me know.
Nov 10 '08 #9
Thansk. I could change the space separated columns into tab separated columns using

Expand|Select|Wrap|Line Numbers
  1. s/\s+/\t/g;
and hence could do further analysis.

Regards
Nov 10 '08 #10

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

138 posts views Thread by ambika | last post: by
5 posts views Thread by edu.mvk | last post: by
6 posts views Thread by edu.mvk | last post: by
38 posts views Thread by edu.mvk | last post: by
2 posts views Thread by bharath.donnipad@gmail.com | last post: by
8 posts views Thread by naveen.dixit@gmail.com | last post: by
51 posts views Thread by deepak | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.