Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: Combine duplicated keys in Hash array

by Marshall (Canon)
on Jul 03, 2020 at 02:48 UTC ( [id://11118846]=note: print w/replies, xml ) Need Help??


in reply to Combine duplicated keys in Hash array

UPDATE: Sorry I think I messed up here. Is this something like you want? My brain is hurting today.
use strict; use warnings; while (<DATA>) { next unless /\S/; chomp; my ($name, $markerA, $markerB, $height) = split (/\s+/,$_); $name //= ""; $markerA //= ""; $markerA ="" if $markerA =~ /^0$/; $markerB //= ""; $markerB ="" if $markerB =~ /^0$/; if ($markerA !~ /A/) { $markerA = $markerB; $markerB = "B"; } $height //= ""; if (!$height) { my $string; foreach ($name, $markerA, $markerB) { $string .= "\'$_\',"; } chop $string; print "$string\n"; } } =prints '2','A','' '3','','B' =cut #Name Marker1 Marker2 Height Time __DATA__ 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
I think this could be done better. Below was a admittedly failed bogus solution for first post.
use strict; use warnings; my %name; while(my $line =<DATA>) { next unless $line =~ /\S/; #skip blank lines chomp $line; my ($undef, $markerName,undef, undef, $height) = split (/\s+/, $line); $name{$markerName} = $height; } foreach (sort keys %name) { print "$_ => $name{$_}\n"; } =prints a => 55 b => 32 c => 34 =cut __DATA__ 1 a 23 99999 55 4 c 55 8888 34 5 b 45 88888 32

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (5)
As of 2024-04-24 04:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found