Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Combine duplicated keys in Hash array

by BillKSmith (Monsignor)
on Jul 03, 2020 at 18:40 UTC ( [id://11118882]=note: print w/replies, xml ) Need Help??


in reply to Combine duplicated keys in Hash array

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

Replies are listed 'Best First'.
Re^2: Combine duplicated keys in Hash array
by Anonymous Monk on Jul 04, 2020 at 23:03 UTC
    Thank you! This most mimics what I'm going for. Could you explain this behalf?
    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];

      My use of push changed the nature of your structure %data from a hash of arrays to a hash of arrays of arrays. (refer: perldsc) Please uncomment my "print Dump" statement and run my code again. Refer to that dump as you read my explanation.

      The keys of the hash %data are the instances of $curr_identifier. The corresponding values are array refs. Each of these refers to an array of two elements (one for each line associated with the curr_identifier). Each of these elements is itself a reference to an array (in fact, each array is an instance of your originally "@result".)

      My for loop iterates through the keys ($curr_identifier) of the hash %data. For each key, it stores the corresponding value (A reference to an array of arrays) as $curr_data. The next four statements, which you explicitly asked about, dereference that reference (Section "Using References" in perlref) to get the two values of height and the two values of markername. I appended a zero or a one to each name to indicate which of the pair of lines it came from.

      Bill

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11118882]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-03-29 09:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found