Add numbers in a string

Discussion in 'Perl Misc' started by abbyhoffman@gmail.com, Nov 19, 2006.

  1. Guest

    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
    , Nov 19, 2006
    #1
    1. Advertising

  2. Joerg Guest

    Am 19.11.2006 20:29 schrieb :
    > 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 );


    --
    Joerg
    Joerg, Nov 19, 2006
    #2
    1. Advertising

  3. Guest

    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,

    Abe
    Lawrence Statton XE2/N1GAK wrote:
    > writes:
    > > test 34bob 232 frank 1

    >
    > #!/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";
    >
    > -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
    > Lawrence Statton - s/aba/c/g
    > Computer software consists of only two components: ones and
    > zeros, in roughly equal proportions. All that is required is to
    > sort them into the correct order.
    , Nov 19, 2006
    #3
  4. Guest

    Hey Guys,

    Thanks for all of your help! I think I'm going to like Perl!

    Abe
    wrote:
    > 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
    , Nov 19, 2006
    #4
  5. John Bokma Guest

    Lawrence Statton XE2/N1GAK <> wrote:

    > writes:
    >> test 34bob 232 frank 1

    >
    > #!/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.

    > Computer software consists of only two components: ones and
    > zeros, in roughly equal proportions. All that is required is to
    > sort them into the correct order.




    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Nov 19, 2006
    #5
  6. DJ Stunks Guest

    John Bokma wrote:
    > 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
    DJ Stunks, Nov 19, 2006
    #6
  7. Sisyphus Guest

    "Lawrence Statton XE2/N1GAK" <> wrote in message
    news:...
    > writes:
    > > test 34bob 232 frank 1

    >
    > #!/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
    Sisyphus, Nov 19, 2006
    #7
  8. wrote:
    >
    > 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
    --
    Perl isn't a toolbox, but a small machine shop where you can special-order
    certain sorts of tools at low cost and in short order. -- Larry Wall
    John W. Krahn, Nov 19, 2006
    #8
  9. Guest

    wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
    , Nov 20, 2006
    #9
  10. John Bokma <> wrote:
    > Lawrence Statton XE2/N1GAK <> wrote:
    >
    >> writes:
    >>> test 34bob 232 frank 1

    >>
    >> #!/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 :)


    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
    Michal Jaegermann, Nov 20, 2006
    #10
  11. [ Please do not top-post.
    Text rearranged into a sensible order.
    ]

    <> wrote:

    > Lawrence Statton XE2/N1GAK wrote:
    >> writes:
    >> > test 34bob 232 frank 1

    >>
    >> #!/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";



    > 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;


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Nov 20, 2006
    #11
  12. John Bokma Guest

    "DJ Stunks" <> wrote:

    > John Bokma wrote:
    >> 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.


    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...

    --
    John Experienced Perl programmer: http://castleamber.com/

    Perl help, tutorials, and examples: http://johnbokma.com/perl/
    John Bokma, Nov 20, 2006
    #12
  13. Mirco Wahab Guest

    Abigail wrote:
    > Mirco Wahab (-halle.de) 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
    Mirco Wahab, Nov 20, 2006
    #13
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Subra
    Replies:
    25
    Views:
    1,169
    user923005
    Mar 8, 2007
  2. Andrew Tatum

    Fibonacci Numbers and Lucas Numbers

    Andrew Tatum, May 26, 2007, in forum: C++
    Replies:
    6
    Views:
    540
    Howard
    May 27, 2007
  3. Lance Hoffmeyer
    Replies:
    2
    Views:
    511
    Lance Hoffmeyer
    Jul 26, 2007
  4. jko170
    Replies:
    9
    Views:
    113
    jko170
    Jan 21, 2009
  5. GIMME
    Replies:
    5
    Views:
    177
    Thomas 'PointedEars' Lahn
    Jul 26, 2004
Loading...

Share This Page