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


In reply to Re: Combine duplicated keys in Hash array by kcott
in thread 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.