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

printing the entire row with particular given last column criteria

by deepakshyl (Novice)
on Jul 31, 2014 at 05:31 UTC ( [id://1095682]=perlquestion: print w/replies, xml ) Need Help??

deepakshyl has asked for the wisdom of the Perl Monks concerning the following question:

INPUT
orf00007					PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1|			  7665 8618	0.210897481636936
orf00007					PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1|			  7665 8618	0.210897481636936
orf00007					PHAGE_Prochl_P_HM2_NC_015284-gi|326783200|ref|YP_004323597.1|			  7665 8618	0.207761175236097
orf00015					PHAGE_Megavi_lba_NC_020232-gi|448825467|ref|YP_007418398.1|			  11594 13510	0.278721920668058
orf00015					PHAGE_Acanth_moumouvirus_NC_020104-gi|441432357|ref|YP_007354399.1|			  11594 13510	0.278721920668058

THE SCRIPT I HAD IMPLEMENTED 

use feature qw/say/; use Math::Trig; open FILE,"out02.txt"; my @file= <FILE>; close FILE; my $aa = 0; for (my $i =$aa; $i <=17822; $i++) { if(($file[$i] >= 0.210)){ open (OUTFILE,'>>out_t10-t10.txt'); print OUTFILE $file[$i]; } else{} }
NOTE: 1) I need to take the last column as the analysing criteria to print the entire row(the float value, eg:0.210897481636936) 2) for example if the user input value is '0.210' we have to print the rows having >= values ,the expected output is OUTPUT orf00007 PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1| 7665 8618 0.210897481636936 orf00007 PHAGE_Prochl_MED4_213_NC_020845-gi|472340344|ref|YP_007673870.1| 7665 8618 0.210897481636936 orf00015 PHAGE_Megavi_lba_NC_020232-gi|448825467|ref|YP_007418398.1| 11594 13510 0.278721920668058 orf00015 PHAGE_Acanth_moumouvirus_NC_020104-gi|441432357|ref|YP_007354399.1| 11594 13510 0.278721920668058
  • Comment on printing the entire row with particular given last column criteria
  • Download Code

Replies are listed 'Best First'.
Re: printing the entire row with particular given last column criteria
by Laurent_R (Canon) on Jul 31, 2014 at 06:22 UTC
    Two things about your script: 1. you should probably chomp your input lines; 2. it would probably be better to open your output file only once, i.e. before entering the for loop.

    Update: and you don't need an empty else block.

Re: printing the entire row with particular given last column criteria
by Anonymous Monk on Jul 31, 2014 at 06:02 UTC

    Solved with a one-liner:

    perl -ne 'print if (split)[-1] >= 0.210' INPUT >> OUTPUT

Re: printing the entire row with particular given last column criteria
by thanos1983 (Parson) on Jul 31, 2014 at 13:47 UTC

    Hello deepakshyl,

    I think the other monks have all ready given you the solution and I would say that also really impressive the solution in one line from Anonymous Monk.

    The reason that I am also writing in this question is to add a small part of explanation on what Laurent_R provided with really good observations.

    Based on my perspective you are not an experienced coder (neither I am) so I think a few things are worth mentioning here and possibly learning.

    1) chomp is most commonly used to remove the newline character "\n" from the end of an input record. It is important to remove these characters while you are reading through a file so you can extract all the strings (data in general) without any faults or extra characters.

    2) Copy from you code:

    use feature qw/say/; use Math::Trig; open FILE,"out02.txt"; my @file= <FILE>; close FILE; my $aa = 0; for (my $i =$aa; $i <=17822; $i++) { if(($file[$i] >= 0.210)){ open (OUTFILE,'>>out_t10-t10.txt'); print OUTFILE $file[$i]; } else{} }

    You are opening the OUTFILE every time the loop occurs! In result you open the file 17822 times!

    As Laurent_R proposed open your file before the loop.

    3) Closing the files!!!! As far as I see you open your READ file and you close it after nice, but your WRITE file you open it 17822 times and you did not close it even once!

    It is very important to close the file after use. You will flush the data into the file and you will prevent data losses.

    I think the best way to open or close a file is to use a die command so you can verify that you opened the file or closed correctly!

    Sample of code taken from the close FILEHANDLE (perldoc.perl.org)

    open(OUTPUT, '|sort >foo') # pipe to sort or die "Can't start sort: $!"; #... # print stuff to output close OUTPUT # wait for sort to finish or warn $! ? "Error closing sort pipe: $!" : "Exit status $? from sort"; open(INPUT, 'foo') # get sort's results or die "Can't open 'foo' for input: $!";

    Hope this information will help and not confuse you more.

    PS: Follow the links that I have provided you with, it will help you to get more information on what I was referring here.

    Seeking for Perl wisdom...on the process...not there...yet!

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1095682]
Approved by 2teez
Front-paged by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (5)
As of 2024-04-19 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found