Your %data hash does not save both @result arrays for each "identifier". Note that I have changed your code to push the array reference into an array rather than storing them directly. I think I am processing the resulting hash correctly - at least it duplicates your desired output. Your output suggests that you were attempting to process the data has inside your loop rather than passing it on as your text indicates. You printed output for every line.
use strict;
use warnings;
use Data::Dumper;
my %data;
while (my $line = <DATA>) {
chomp $line;
next if $line eq '';
my @data = split ("\t", $line);
my $curr_identifier= $data[0];
my $markername= $data[1];
my $position1= $data[2];
my$height= $data[4];
my @result;
if ($. > 1){
$result[0] = $markername; # line[1]
$result[1] = $position1; # line[2]
$result[2] = $height; # line[4]
$result[3] = $curr_identifier; # line[0]
#$data{$curr_identifier}= [@result];
push @{$data{$curr_identifier}}, [@result];
}
}
#print Dumper(\%data);
foreach my $curr_identifier (sort keys %data) {
my $curr_data = $data{$curr_identifier};
# Neither defined
if (!defined $curr_data->[0][2] and !defined $curr_data->[1][2] )
+{
print "$curr_identifier - No height for either marker - failed
+\n";
next;
}
# Both defined
my $output0;
my $output1;
my $height_0 = $curr_data->[0][2];
my $height_1 = $curr_data->[1][2];
my $markername_0 = $curr_data->[0][0];
my $markername_1 = $curr_data->[1][0];
if ( defined $height_0 and defined $height_1 ) {
$output0 = $curr_data->[0][0];
$output1 = $curr_data->[1][0];
}
else {
# Only one defined
$output0 = (!defined $height_0) ? $markername_0 : q('');
$output1 = (!defined $height_1) ? $markername_1 : q('');
}
print "$curr_identifier , $output0 , $output1\n";
}
__DATA__
Name Marker Position1 Height Time
1 A A 6246 0.9706
1 B B 3237 0.9706
2 A 0
2 B B 5495 0.9775
3 A A 11254 0.9694
3 B 0
4 B B
4 A A
OUTPUT:
1 A B
2 A ''
3 '' B
4 - No height for either marker - failed