Skip to content

Instantly share code, notes, and snippets.

@Puriney
Created February 3, 2013 09:47
Show Gist options
  • Save Puriney/4701087 to your computer and use it in GitHub Desktop.
Save Puriney/4701087 to your computer and use it in GitHub Desktop.
PERL: Sum of Number String
#!/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