On Thursday 18 Sep 2003 10:26 am, Marcus Claesson wrote:
> Hi People,
>
> I have a silly little list-parsing problem that I can't get my head
> around, and I'm sure some of you have come across it before.
>
> I have a list like this:
>
> 1     a
> 2     b
> 2     c
> 3     a
> 4     d
> 4     d
> 4     e
> 4     f
> 5     g
>
> and I want to make the first column non-redundant and collect the second
> column values on the same line, like this:
>
> 1     a
> 2     b,c
> 3     a
> 4     d,e,f
> 5     g
>
> Please note that line 4 only has one 'd'.
>
> I've tried with both hashes and arrays (don't want to confuse you so I
> won't display them here), but nothing really works...
>
> I would really appreciate any help!
>
> Marcus

Hi Marcus,

Hows about something like:

my @f=("1       a","2       b","2       c",
       "3       a","4       d","4       d",
       "4       e","4       f","5       g");

my %cols=();
foreach(@f) {
  my ($col1,$col2)=split(/ */,$_);
  $cols{$col1}.=$col2;
}
foreach (sort keys %cols) {
  print join(",",split('',$cols{$_})),"\n";
}

If you actually want the commas in the scripts, you'll need to do it either 
inside the first loop, or have a seperate loop just after.

Gary
-- 
Gary Stainburn
 
This email does not contain private or confidential material as it
may be snooped on by interested government parties for unknown
and undisclosed purposes - Regulation of Investigatory Powers Act, 2000     


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to