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
Bill

In reply to Re: Combine duplicated keys in Hash array by BillKSmith
in thread Combine duplicated keys in Hash array by Tikplay

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.