Update Oops. I didn't see the "preserve order" bit. In that case, this won't do exactly what you want. Sorry. See the other (better) solutions...

This should do what you want...

use strict; my %data = (); while (<DATA>) { chomp; my ($col1, $col2) = split /\s+/; $data{$col1} .= "$col2 "; } print "$_: $data{$_}\n" foreach (sort keys %data); __DATA__ text1 text-a text2 text-b text3 text-c text1 text-d text3 text-e text3 text-f
Here's the explanation:
  1. The while loop goes through each line of input. In this case, each line in <DATA>.
  2. The line is then split on any whitespace into the two columns.
  3. To ensure uniqueness, the first column is used as the key name in a hash and the second column is appended to that.
  4. Then it prints each key, and its value using a foreach statement. The keys are also sorted.

In reply to Re: Removing redundancy by The Mad Hatter
in thread Removing redundancy by dr_jgbn

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.