Beefy Boxes and Bandwidth Generously Provided by pair Networks
go ahead... be a heretic
 
PerlMonks  

Re: Combine duplicated keys in Hash array

by kcott (Archbishop)
on Jul 03, 2020 at 06:29 UTC ( [id://11118849]=note: print w/replies, xml ) Need Help??


in reply to Combine duplicated keys in Hash array

G'day Tikplay,

Welcome to the Monastery.

There's a number of issues with your post which makes providing an answer difficult. You seem to be relying on "dynamically scoped variables" which could be causing you problems; however, as you haven't provided sufficient code, that's just a guess. Take a look at perlintro, "How do I post a question effectively?" and SSCCE.

You've posted the code you did provide in <code> tags, which is good; however, you should also do the same for your data. HTML renders consecutive whitespace characters as a single space: all of the tabs in your input are lost. The code below just uses those spaces for the DATA; the output uses tabs. I also added a couple of dummy lines to test the "failed" logic.

As best as I can determine, this code performs the basic functionality you want:

#!/usr/bin/env perl use strict; use warnings; my %data; while (<DATA>) { chomp; next unless length; my ($id, $height) = (split)[0,3]; $height = ' ' unless defined $height; push @{$data{$id}}, $height; } my $format = "'%d', '%s',\t'%s'\n"; for my $id (sort { $a <=> $b } keys %data) { if ($data{$id}[0] eq ' ' && $data{$id}[1] eq ' ') { print "$id: both height markers missing - failed\n"; } else { printf $format, $id, @{$data{$id}}; } } __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 4 A 4 B

Here's the output:

'1', '6246', '3237' '2', ' ', '5495' '3', '11254', ' ' 4: both height markers missing - failed

— Ken

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others rifling through the Monastery: (6)
As of 2024-04-16 05:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found