Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

extracting single column

by Anonymous Monk
on Feb 09, 2012 at 12:44 UTC ( [id://952702]=perlquestion: print w/replies, xml ) Need Help??

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

HI all, This is the code that i have used to extract a particular column from text file.

@arr_file=("R25_s_5_seq_18-26_alignment.txt","R20_s_6_seq_18-26_alignm +ent.txt"); foreach $file in @arr_file { open(FILE, $file); while(defined($line=<FILE>)) { @arr_words=split(/\s+/,$line); print FINAL $arr[2]; } close FILE; } close FINAL;
however it is showing some complation errors plz can any one help me out The file looks like
HWUSI-EAS570R_0000:7:1:5348:9121#0/1 + mmu-miR-16-1-3p 8 TTGACTGTGCTGCTGA IIIIIIIIIIIIIIII 0
and i need to extract only "mmu-miR-16-1-3p"/p> part

Replies are listed 'Best First'.
Re: extracting single column
by moritz (Cardinal) on Feb 09, 2012 at 12:56 UTC

    The solution is to learn Perl, and to understand what you are doing. Seriously, you can't just make up some syntax and hope that perl will magically convert it into a running, correct program.

    Check perlsyn or perlintro for the syntax of the foreach loop.

    Writing to FINAL (which you've not opened) is going to be disappointing at run time. use warnings; will catch that (see perllexwarn), and please enable use strict; too. See for example RFC: Tutorial: use strict; now what!? for instructions.

Re: extracting single column
by Marshall (Canon) on Feb 09, 2012 at 13:02 UTC
    Perhaps...
    #!/usr/bin/perl -w use strict; my $somepath = "path to write to.."; my @arr_file=("R25_s_5_seq_18-26_alignment.txt", "R20_s_6_seq_18-26_alignment.txt"); open (FINAL, '>', $somepath) or die "cannot open $somepath for write $!"; foreach my $file (@arr_file) { open (FILE, '<', $file) or die "cannot open $file for reading $!"; while ( my $line=<FILE> ) { my ($important_thing) = (split(/\s+/,$line))[2]; print FINAL "$important_thing\n"; } }
    This is actually just a command line grep.
    Type "man grep" at the command prompt.
Re: extracting single column
by choroba (Cardinal) on Feb 09, 2012 at 12:46 UTC
    it is showing some complation errors
    can u plz show these errrs so we can help u

      Bareword found where operator expected at Average.pl line 3, near "$file in" (Missing operator before in?)
      Array found where operator expected at Average.pl line 3, at end of line (Do you need to predeclare in?)
      syntax error at Average.pl line 3, near "$file in "
      syntax error at Average.pl line 12, near "}"
      Execution of Average.pl aborted due to compilation errors
      .

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://952702]
Approved by ww
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2024-04-23 10:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found