#!/usr/bin/perl use strict; # We want to do the following: #counting starts at 0 # the 0 , 1 digits shoud be untouched # if the 2 digit is a 0 and the next digit is a 0 remove all 0 untill first non 0 # if the 2 digit a 0 and the next is a non 0 remove the 0 # if the 3 digit is a 0 and follows by a 0 then remove all 0 except the 0 at 3 digit # if the 3 digit is a 0 and the next digit is a non 0 remove only the 0 # if the 4 digit is a 0 remove it and all following 0 untill non 0 foreach my $number () { chomp($number); # print $number . "\n"; my @digits = split("", $number); my $newnumber; my $i = 0; for ($i=0; exists($digits[$i]); $i++) { # print "I:$i, " . $digits[$i] . "\n"; if ($i < 2) { # 0, 1 digits should be untouched $newnumber .= $digits[$i]; next; } elsif ($i == 2) { # if the 2 digit is a 0 and the next digit is a 0 remove all 0 untill first non 0 # if the 2 digit a 0 and the next is a non 0 remove the 0 if ($digits[$i] > 0) { $newnumber .= $digits[$i]; next; } elsif (($digits[2] == 0) && ($digits[3] == 0)) { for(my $j = 2; $digits[$j] == 0; $j++) { $i++; # we skip this digit } $newnumber .= $digits[$i]; } # no need for another else b/c we remove the 0 anyway } elsif ($i ==3) { # if the 3 digit is a 0 and follows by a 0 then remove all 0 except the 0 at 3 digit # if the 3 digit is a 0 and the next digit is a non 0 remove only the 0 if (($digits[3] == 0) && ($digits[4] > 0)) { next; } elsif(($digits[3] == 0) && ($digits[4] == 0)){ $newnumber .= $digits[$i]; for (my $j = 4; $digits[$j] == 0; $j++) { $i++; } $newnumber .= $digits[$i]; } } elsif ($i == 4) { # if the 4 digit is a 0 remove it and all following 0 untill non 0 if ($digits[4] == 0) { for (my $j=4; $digits[$j] == 0; $j++) { $i++; } $newnumber .= $digits[$i]; } else { $newnumber .= $digits[$i]; } } else { $newnumber .= $digits[$i]; } } print "$number -> $newnumber\n"; } __DATA__ 215000007801 300000324002 890000457651 210004563401 201045139158