Add numbers in a string

A

abbyhoffman

Hello,

I'm a complete Perl newb, but I was wondering if there was a way to add
the numbers in a string together. Ie

test 34bob 232 frank 1

would add 34 + 232 + 1 to get 267

Thanks,

Abe
 
J

Joerg

Am 19.11.2006 20:29 schrieb (e-mail address removed):
I'm a complete Perl newb, but I was wondering if there was a way to add
the numbers in a string together. Ie

test 34bob 232 frank 1

would add 34 + 232 + 1 to get 267

Hi,
a short way:

my $sum=0;
my $string="test 34bob 232 frank 1";
$sum+=$1 while( $string=~/(\d+)/g );
 
A

abbyhoffman

Lawrence,

That worked great for the example, but is there a way I can send a
generic string? ie

$text = "test 34bob 232 frank 1";
my @words = qw / $text /;
....

Thanks,
 
J

John Bokma

Lawrence Statton XE2/N1GAK said:
#!/usr/bin/perl
use strict;
use warnings;

my @words = qw / test 34bob 232 frank 1 /;
my $sum;
$sum += $_ for grep { /^\d+$/ } map { tr /0-9//cd; $_ } @words;
print "$sum\n";

Way to weird :)


use strict;
use warnings;

my $string = 'test 34bob 232 frank 1';
my $sum = 0;
# add numbers to the sum while we find numbers in the string
$sum += $1 while $string =~ /(\d+)/g;
print "$sum\n";

C:\Documents and Settings\John\My Documents>sum.pl
267

A sig starts with -- followed by exactly one space on a line on itself.
 
D

DJ Stunks

John said:
use strict;
use warnings;

my $string = 'test 34bob 232 frank 1';
my $sum = 0;
# add numbers to the sum while we find numbers in the string
$sum += $1 while $string =~ /(\d+)/g;
print "$sum\n";

C:\Documents and Settings\John\My Documents>sum.pl
267

no need to capture in the regex. and how about adding them all up at
once?

#!/usr/bin/perl

use strict;
use warnings;

use List::Util qw{ sum };

my $string = 'test 34bob 232 frank 1';
print sum $string =~ m{ \d+ }xg;

__END__

-jp
 
S

Sisyphus

Lawrence Statton XE2/N1GAK said:
#!/usr/bin/perl
use strict;
use warnings;

my @words = qw / test 34bob 232 frank 1 /;
my $sum;

# Then simply:
$sum += $_ for @words;

If you want to hide the warnings:

{
no warnings 'numeric';
$sum += $_ for @words;
}

Cheers,
Rob
 
J

John W. Krahn

I'm a complete Perl newb, but I was wondering if there was a way to add
the numbers in a string together. Ie

test 34bob 232 frank 1

would add 34 + 232 + 1 to get 267

$ perl -le'
my $string = q[test 34bob 232 frank 1];
my $total;
1 while $string =~ /(\d+)(?{ $total += $^N })/g;
print $total;
'
267



John
 
X

xhoster

Hello,

I'm a complete Perl newb, but I was wondering if there was a way to add
the numbers in a string together. Ie

test 34bob 232 frank 1

would add 34 + 232 + 1 to get 267

perl -le 'use List::Util qw(sum);print sum /\d+/g while <>'

Xho
 
M

Michal Jaegermann

John Bokma said:
Way to weird :)

Awk is simpler here if strigs are in suffixes. :)

echo test 34bob 232 frank 1 | \
awk '{ for(i=1; i <= NF; i++) {sum += $i}; print sum }'

It will accept floating point numbers with appended strings too.

Michal
 
T

Tad McClellan

[ Please do not top-post.
Text rearranged into a sensible order.
]

That worked great for the example, but is there a way I can send a
generic string? ie

$text = "test 34bob 232 frank 1";


You can convert the string into the needed form of array:

my @words = split /\s+/, $text;
 
J

John Bokma

DJ Stunks said:
no need to capture in the regex.

Use of uninitialized value in addition (+) at C:\Documents and Settings
\John\My Documents\sum.pl line 6.
Use of uninitialized value in addition (+) at C:\Documents and Settings
\John\My Documents\sum.pl line 6.
Use of uninitialized value in addition (+) at C:\Documents and Settings
\John\My Documents\sum.pl line 6.
0

I guess you mean that:
and how about adding them all up at
once?

:-D. After posting the "while version" I thought, maybe nice for a blog
entry, and maybe another example using List::Util would be cool...
 
M

Mirco Wahab

Abigail said:
Mirco Wahab ([email protected]) wrote on MMMMDCCCXXIX September
][ Thus spoke Abigail (on 2006-11-20 01:47):
][ > my $str = '34 + 232 + 1';
][ > my @num = $str =~ /\d+/g;
][ > $" = " + ";
][ > print eval "@num";
][
][ Nice Idea!
][
][ but much too verbose (;-)
][
][ $" = " + ";
][ print eval "@$_" for [$string =~ /\d+/g];


$" = " + "
$_ = "34 + 232 + 1";
print eval "@{[/\d+/g]}";


WTF.

I didn't expect sth. like that would work.

(It does.)

Regrds & thanks for this one.

Mirco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top