my @result = split '', 'abcdefghijklmno'; my @arg1 = (0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15); my %rules; # Build the rules hash $rules{$arg1[$_]} = $result[$_] for 0 .. $#result; my @newseq = (); my $run = 0; my @bin = (); while ( ) { @bin= split(/ /, $_); for (my $i=0;$i<$#bin;$i++) { if ($bin[$i]==0){ $run=0; push @newseq,$run; } if ($bin[$i]==1 && $bin[$i+1]==0){ $run++; push @newseq, $run; } else { $run++; } } } for (1 .. $#newseq) { if ( $_ % 80 != 0){ print "$rules{$newseq[$_-1]}"} else{ print "$rules{$newseq[$_-1]}\n"; } }