in reply to Creating Minimal Subgroups in a List of Characters

Does this do what you want?
#!perl use strict; # First, group them by the first part of the numeric field # and the other two columns # then find how much of the rest of the numeric field is needed # to distinguish the variations in the third column my %grouping; while(<DATA>) { my ($n, $n2, $c2, $c3) = /(\d+)\.(\S+)\s+(\w+)\s+(\w+)/; $grouping{$n}{$c2}{$c3}{$n2} = $_; } for my $n (sort {$a <=> $b} keys %grouping) { my $N = $grouping{$n}; for my $c2 (sort keys %$N) { my $C2 = $N->{$c2}; if (keys %$C2 > 1) { # print "$n$c2 is a complex group, containing:\n"; for my $c3 (sort keys %$C2) { my $C3 = $C2->{$c3}; # print "..(for c3=$c3):\n"; for my $n (sort keys %$C3) { print "$C3->{$n}"; } } } else { print "$n$c2 contains:\n"; my $C3 = $C2->{(keys %$C2)[0]}; for my $n (sort keys %$C3) { print " $C3->{$n}"; } } } } __DATA__ 0024.118.3A FT TFF 0024.118.3B FT TFF 0024.118.3C FT TFF 0024.118.3D FT TFF 0024.118.4 FF TFF 0039.04 NN OTH 0039.205.2 FT OTH 0039.205.6 FT TFF 409.176.12A FT TFF 409.176.12B FT TFF 409.176.12C FT TFF 409.176.12D FT OTH 409.176.12E FT OTH

Caution: Contents may have been coded under pressure.

Replies are listed 'Best First'.
Re^2: Creating Minimal Subgroups in a List of Characters
by periapt (Hermit) on Sep 03, 2004 at 12:29 UTC

    This is great. It certainly seems to work. I'll have to expand it out to handle the full problem but it looks like a good solution.

    I actually coded up a different solution using hashes that works well. If your interested, you can look at the production code on my scratchpad here.

    Thanks for your help

    PJ
    use strict; use warnings; use diagnostics; (if needed)