in reply to How to map data from one column based on another column in perl
So, as a clarification step, I wrote code to print the columns of interest (the ones that you mentioned).
In the code below, I translated the column indices into names based upon your post. Usually it is a good idea to use a meaningful name for a column instead of a number just because that is much easier for humans to understand. Perl is very flexible in how this can be done.
Please explain what the desired output is for each line below.
Also, in my opinion a tab delimited file is a difficult critter to deal with because it is difficult to see the difference between space and tab. I guess you are exporting this from an Excel table? Better is to use standard CSV or I often use pipe "|" delimiters for files that may be subsequently modified by humans. Humans cannot be expected to write correct syntax for columns containing embedded commas when using a comma delimited file. However, "|" delimited files work well because this character is not part of names or addresses. This tab delimited stuff is just hard to deal with.
UPDATE:#/usr/bin/perl use strict; use warnings; $|=1; #turns stdio buffer off so that error line order is correct while (defined (my $line = <DATA>)) { chomp $line; #removes end of line character(s) next unless $line =~ /\S/; # skip blank lines my ($motherId,$yearBirth,$individualId) = (split /\t/,$line)[4,13, +2]; $yearBirth //= ""; # DATA line 6 is a bit weird # this defines $yearBirth as "" if undefined print "UniqId=$individualId Mother=$motherId BirthY=$yearBirth \ +n"; } =Prints UniqId=1 Mother=3 BirthY=51 UniqId=2 Mother= BirthY= UniqId=3 Mother=36 BirthY=65 UniqId=4 Mother=3 BirthY=50 UniqId=5 Mother=3 BirthY=47 UniqId=6 Mother=3 BirthY= UniqId=7 Mother=3 BirthY= UniqId=8 Mother=3 BirthY=42 UniqId=9 Mother=3 BirthY=39 UniqId=10 Mother=3 BirthY=35 UniqId=11 Mother=8 BirthY=11 UniqId=12 Mother=8 BirthY=9 =cut __DATA__ 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
#/usr/bin/perl use strict; use warnings; $|=1; #turns stdio buffer off so that error line order is correct while (defined (my $line = <DATA>)) { chomp $line; #removes end of line character(s) next unless $line =~ /\S/; # skip blank lines my ($motherId,$yearBirth,$individualId,$what) = (split /\|/,$line) +[4,13,2,14]; $yearBirth //= ""; # DATA line 6 is a bit weird # this defines $yearBirth as "" if undefined $what //= ""; # I don't know what this is? print "UniqId=$individualId Mother=$motherId BirthY=$yearBirth W +hat=$what\n"; } =Prints UniqId=1 Mother=3 BirthY=51 What= UniqId=2 Mother= BirthY= What=999 UniqId=3 Mother=36 BirthY=65 What= UniqId=4 Mother=3 BirthY=50 What=50 UniqId=5 Mother=3 BirthY=47 What=46 UniqId=6 Mother=3 BirthY= What= UniqId=7 Mother=3 BirthY= What=999 UniqId=8 Mother=3 BirthY=42 What= UniqId=9 Mother=3 BirthY=39 What= UniqId=10 Mother=3 BirthY=35 What= UniqId=11 Mother=8 BirthY=11 What= UniqId=12 Mother=8 BirthY=9 What= =cut __DATA__ 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 |||||||||999 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|
|
|---|