Tikplay has asked for the wisdom of the Perl Monks concerning the following question:
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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Combine duplicated keys in Hash array
by kcott (Archbishop) on Jul 03, 2020 at 06:29 UTC | |
|
Re: Combine duplicated keys in Hash array
by Marshall (Canon) on Jul 03, 2020 at 02:48 UTC | |
|
Re: Combine duplicated keys in Hash array
by BillKSmith (Monsignor) on Jul 03, 2020 at 18:40 UTC | |
by Anonymous Monk on Jul 04, 2020 at 23:03 UTC | |
by BillKSmith (Monsignor) on Jul 05, 2020 at 19:00 UTC |