Hello, I’m having issues with this and wondering if someone could provide some help. I'm parsing a .txt file and want to combine duplicated keys and it's values. Essentially, for each identifier I want to store it's height value. Each "sample" has 2 entries (A & B). I have the file stored like this:

while(...){ @data= split ("\t", $line); $curr_identifier= $data[0]; $markername= $data[1]; $position1= $data[2]; $height= $data[4]; if ($line >0){ $result[0] = $markername; $result[1] = $position1; $result[2] = $height; $result[3] = $curr_identifier; $data{$curr_identifier}= [@result]; } }

This seems to work fine, but my issue is that when I send this data to below function. It prints the $curr_identifier twice. I only want to populate unique identifiers and check for the presence of it's $height variable.

if (!defined $data{$curr_identifier}[2]){ $output1= "no height for both markers- failed"; } else { if ($data{$curr_identifier}[2] eq " ") { $output1 = $markername; } } print $curr_identifier, $output1 . "\t" . $output1 . "\n";

Basically, if sample height is present for both markers (A&B), then output is both markers. '1', 'A', 'B'

If height is not present, then output is empty for reported marker.

'2', 'A', ' '

'3', ' ', 'B'

My current output is printing out like this:

1, A

1, B

2, A

2, ' '

3, ' '

3, B'

_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


In reply to 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.