Created
February 3, 2013 09:47
-
-
Save Puriney/4701087 to your computer and use it in GitHub Desktop.
PERL: Sum of Number String
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/perl -w | |
# string of number | |
# sum number larger than 0 | |
# report start and end site | |
my $line = shift; | |
my @numbers = (split //,$line); | |
my $tag = 0; | |
my $sum = 0; | |
my $start; | |
my $end; | |
for (my $i = 0; $i <= @numbers-1;$i++ ) { | |
if ($numbers[$i] > 0 ){ | |
$sum = $sum +$numbers[$i]; | |
if ($tag ==0){ | |
$tag =1; | |
$start = $i; | |
$end = $i; | |
} | |
elsif ($tag ==1){ | |
$end ++; | |
} | |
if ( $i == @numbers -1){ | |
print "$start\t$end\t$sum\n"; | |
} | |
} | |
else { | |
if ($tag == 1){ | |
print "$start\t$end\t$sum\n"; | |
$sum =0; | |
$tag = 0; | |
} | |
elsif ($tag ==0){ | |
($start, $end, $sum) = (0,0,0); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment