waekit has asked for the wisdom of the Perl Monks concerning the following question:
Hi, please help. I'm very new to Perl and coding, I have a big file with 15 columns and some with missing information. What I'm trying to do, is select column [ 13] with missing information and column [ 14] with no missing information; and then use column [ 4] as a key to map to column [ 2] and print all lines in column [ 2].
Here's how my data file looks like (tab delimited):
BC000 1 1 2 3 F 3 51 51 + BC000 0 2 M 999 BC000 0 3 37 36 F 65 + BC000 0 4 2 3 M 50 +50 BC000 0 5 2 3 F 45 47 + 46 BC000 0 6 2 3 F 3 42 + BC000 0 7 2 3 M 99 +9 BC000 0 8 2 3 F 3 42 + BC000 0 9 2 3 F 1 39 + BC000 0 10 2 3 F 3 35 + BC000 0 11 45 8 M 11 + BC000 0 12 45 8 F 9
Basically: column [ 13] = year of birth column [ 4] = motherID column [ 2] = individualID All motherIDs will have an individualID (the same as motherID) Here's my code:
#!/usr/bin/perl use warnings; use strict; my $filename = 'pedigree_proband_testfile.txt'; open (FILE, "<", $filename) or die "Cannot open file $!"; my @data = <FILE>; my @column; my $motherID; foreach my $line (@data) { @column = split ( /\t/, $line); if ($column[13] eq "" && $column[4] ne "" ) { $motherID = join($column[2],$column[4]); if ("$motherID" eq "$column[2]") { print "$line\t\n"; } } }
I tried this but it doesn't return any values. Can you please help? I'm really new to this, I'm trying to write a script to avoid mapping the columns manually in MS Excel as it's a big file with over 4000 rows. Many many thanks in advance!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to map data from one column based on another column in perl
by NetWallah (Canon) on Jun 06, 2018 at 05:39 UTC | |
|
Re: How to map data from one column based on another column in perl
by hippo (Archbishop) on Jun 06, 2018 at 08:01 UTC | |
|
Re: How to map data from one column based on another column in perl
by Marshall (Canon) on Jun 06, 2018 at 19:41 UTC | |
|
Re: How to map data from one column based on another column in perl
by NetWallah (Canon) on Jun 06, 2018 at 18:19 UTC |