Hi guys, I'm a newbie in a programming world, i've been trying to work on this code for last two weeks with no avail. I have thousands of tab delimited files with same format for example
file 1 col0 col1 col2 col3 col4 col5 ... ... samp1 samp2 samp3 samp4 .... files follow similar format and what i need to do is extract 1st and 4 +th column from the first file and output on a new file and then take +out only 4th column from the rest of the files. So before i work on my actual project, i wanted to try with more simpl +e table. The table i'm working on right now is S1.txt col1 col2 col3 1 4 7 2 5 8 3 6 9 S2.txt col1 col2 col3 1 44 77 2 55 88 3 66 99 The result i'm getting col1 col3 col3 1 4 2 5 3 6 The result i want col1 col3 col3 1 4 77 2 5 88 3 6 99
#!/usr/bin/perl -w use warnings; use strict; my @desired_cols = qw(colname1 colname3); my @desired_cols1= qw(colname3); my $temp = 'tmp.txt'; # reads first line to get actual column names open(S1, 'S1.txt') || die "Can't open S1: $!"; open(S2, 'S2.txt') || die "Can't open S2 : $!"; open(OUT, ">$temp") || die "Can't create output : $!"; my $header_line = (<S1>); my $header_line1 = (<S2>); my @actual_cols = split(/\s+/,$header_line); # get column number of the actual column names my $pos =0; my %col2_num = map {$_ => $pos++}@actual_cols; # translate the desired col names into position numbers my @slice = map{$col2_num{$_}}@desired_cols; my @slice1 = map{$col2_num{$_}}@desired_cols1; print OUT join("\t","@desired_cols"),"\r\n"; #header line # print colname1 colname3 colname3 in outfile while (<S1>, <S2>) { my @row = (split)[@slice]; my @row1 = (split)[@slice1]; print OUT join("\t","@row @row1"),"\r\n"; #each data row }
I think the problem with my code is with the while loop, i thought it would read S1 and S2 line by line but it reads S1 only i think.. please help me out, i'm under a lot of stress :((

In reply to Adding a column to a file where i took out two columns by coolda

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.