in reply to working with files

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