From your sample data/output it's not quite clear to me what you mean by "group on A and D"... but here's something that groups by A alone:

#!/usr/bin/perl my %groups; while (<DATA>) { my ($grp, @other) = split ' '; for my $field (@other) { my ($name, $val) = split /:/, $field; if ($name eq "STR") { $groups{$grp}{$name} = $val unless defined $groups{$grp}{$ +name}; # first } elsif ($name eq "END") { $groups{$grp}{$name} = $val; # last } elsif ($name eq "CNT") { $groups{$grp}{$name} += $val; # sum } else { $groups{$grp}{$name}{$val}++; # unique values } } } #use Data::Dumper; # debug #print Dumper \%groups; for my $grp (sort keys %groups) { my $fields = $groups{$grp}; print $grp; for my $name (qw(B C D)) { my @elems = keys %{ $fields->{$name} }; my $val = @elems > 1 ? "MULTIPLE" : $elems[0]; print " $name:$val"; } for my $name (qw(CNT STR END)) { print " $name:$fields->{$name}"; } print "\n"; } __DATA__ A:1 B:2 C:3 D:4 CNT:1 STR:1 END:2 A:1 B:7 C:3 D:4 CNT:1 STR:2 END:3 A:1 B:2 C:3 D:4 CNT:1 STR:3 END:4 A:2 B:2 C:3 D:5 CNT:1 STR:4 END:5 A:2 B:2 C:3 D:5 CNT:5 STR:5 END:10 A:3 B:2 C:3 D:4 CNT:1 STR:11 END:12

Output:

A:1 B:MULTIPLE C:3 D:4 CNT:3 STR:1 END:4 A:2 B:2 C:3 D:5 CNT:6 STR:4 END:10 A:3 B:2 C:3 D:4 CNT:1 STR:11 END:12

(STR and END are assumed to be sorted on input — in case they aren't, you'd need to compute the minimum and maximum value instead of taking the first and the last...)


In reply to Re^3: working with files by almut
in thread working with files by smanicka

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.