coolda has asked for the wisdom of the Perl Monks concerning the following question:
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
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 :((#!/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 }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Adding a column to a file where i took out two columns
by GrandFather (Saint) on Sep 24, 2014 at 02:23 UTC | |
by coolda (Novice) on Sep 24, 2014 at 13:50 UTC | |
by GrandFather (Saint) on Sep 24, 2014 at 21:25 UTC | |
|
Re: Adding a column to a file where i took out two columns
by Tux (Canon) on Sep 25, 2014 at 06:19 UTC |