sub compressPairsWithCount(&@) { my $cr=shift; my @aResult; my $x; my @aRuns=(0); my $i=defined($_[0])?-1:0; my $j=0; local $a=undef; local $b=undef; foreach (map { if ((defined($x) && defined($_) && ($x eq $_)) || (!defined($x) && !defined($_))) { $aRuns[$i]++; () } else { $aRuns[++$i]=1; ($x = $_); } } @_) { $a=$b; $b=$_; $_=$aRuns[$j++]; push @aResult, $cr->(); } return @aResult; } @aCompressed = compressPairsWithCount { '(' . (defined($a)?$a:'') . ',' . (defined($b)?$b:'') . "=$_)" } @aData; print "compressPairsWithCount: @aCompressed\n"; #outputs compressPairsWithCount: (,a=4) (a,b=1) (b,c=2) (c,a=2) (a,d=1) (d,e=4) (e,=2) (,f=1) (f,g=2)