473,385 Members | 1,587 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

doubt in if statement

89
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
9 1684
KevinADC
4,059 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
lilly07
89
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
4,059 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
lilly07
89
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
4,059 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
lilly07
89
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
410 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
lilly07
89
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
lilly07
89
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

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

Similar topics

138
by: ambika | last post by:
Hello, Am not very good with pointers in C,but I have a small doubt about the way these pointers work.. We all know that in an array say x,x is gonna point to the first element in that...
2
by: VMI | last post by:
I'm having doubts as to how the compiler interprets this If statement: bool bIsTrue = true; if (! bIsTrue) { //RUN PROCESS } Here, will "RUN PROCESS" be executed? Or is this just wrong?...
5
by: edu.mvk | last post by:
Hi Friends, I have subscribed to this group recently. I have a doubt in templates in C++ A fraction of code given in "The C++ Programming Language" -By B.Stroustrup is as follows : ...
6
by: edu.mvk | last post by:
Hi, in the below statement : class A { int *i; A(){ };
38
by: edu.mvk | last post by:
Hi I am using strcpy() in my code for copying a string to another string. i am using static char arrays. for the first time it is exected correctly but the second time the control reaches...
2
by: bharath.donnipad | last post by:
Hi, I just started learning c++ since 2 months...I have a following doubt. can someone pls explain. //include headers int main() { int xyz; using namespace std;
8
by: naveen.dixit | last post by:
I have a piece of code #include<iostream> using namespace std; main() { int x=12; int *p = &x; cout<<endl<<p<<endl<<*p++<<endl; cout<<endl<<p<<endl<<*p<<endl;
2
by: tim007 | last post by:
Hi all, I have a doubt regarding the "Using" statement if i write a code like this.... using (sqlDataReader) { do something ....
51
by: deepak | last post by:
Hi, For the program pasted below, scanf is not waiting for the second user input. Can someone suggest reason for this? void main() { char c;
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.