If your input file is sorted in the right way, then each line can just be processed as it is read. Since your test data set was sorted in that way, I took advantage of that to simplify the code. If that's not a good assumption, then of course things get more complicated.
#!/usr/bin/perl use strict; use warnings; $|=1; my $line =<DATA>; my ($a,$b,$c,$d,$cnt,$str,$end) = ($line =~ m/:(\d+)/g); while (my $line2 = <DATA>) { my ($A,$B,$C,$D,$Cnt,$Str,$End) = ($line2 =~ m/:(\d+)/g); if ($a == $A and $d == $D) { $b ="multiple" if ($b ne $B); $c ="multiple" if ($c ne $C); $cnt+=$Cnt; $end=$End; next; } print "A:$a B:$b C:$c D:$d CNT:$cnt STR:$str END:$end\n"; ($a,$b,$c,$d,$cnt,$str,$end)=($A,$B,$C,$D,$Cnt,$Str,$End); } print "A:$a B:$b C:$c D:$d CNT:$cnt STR:$str END:$end\n"; =prints with your test dataset: 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 with another dataset of: 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:8 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 that prints: A:1 B:multiple C:3 D:4 CNT:2 STR:1 END:3 A:1 B:2 C:3 D:8 CNT:1 STR:3 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 =cut __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

In reply to Re: working with files by Marshall
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.