adding strings, not concatenating them

Discussion in 'Perl' started by Stan Horwitz, Feb 15, 2006.

  1. Stan Horwitz

    Stan Horwitz Guest

    I am working on a perl program where I extract a variable from a line of
    input and I want to keep a running count of that variable. I am using
    the substr function to extract the variable. For purposes of example,
    let's say the extracted variable is called $extracted_variable and my
    total variable is called $total. The $extracted_variable comes from
    something along the lines of $extracted_variable = substr($stuff, 0, 8);


    $total = $total + $extracted_variable;

    Let's say $total is initialized to a value of 10 and
    $extracted_variable is "900".

    I want the result to be 910. I do not want a result of 90010.

    How do I do that? My current attempts all result in errors such as

    isn't numeric in addition (+) at

    and after spending an hour querying books on perl and googling, I am
    getting nowhere.

    I am sure this is a simple matter, but what's the trick? Do I have to
    convert each element in the string to a number? I thought perl just did
    that on the fly.
    Stan Horwitz, Feb 15, 2006
    #1
    1. Advertising

  2. Stan Horwitz

    Joe Smith Guest

    Stan Horwitz wrote:

    > $total = $total + $extracted_variable;
    >
    > Let's say $total is initialized to a value of 10 and
    > $extracted_variable is "900".
    >
    > I want the result to be 910. I do not want a result of 90010.
    >
    > How do I do that? My current attempts all result in errors such as
    >
    > isn't numeric in addition (+) at


    Oh, really?

    You need to post a short but complete perl program that
    demonstrates this problem before we can help you. I cannot
    reproduce the problem from your description.

    use warnings;
    $stuff=" 900xyz";
    $extracted_variable = substr $stuff,0,8;
    $total = 10;
    print "Before: total='$total' extracted_variable='$extracted_variable'\n";
    $total = $total + $extracted_variable;
    print "After: total='$total'\n";

    -Joe
    Joe Smith, Feb 15, 2006
    #2
    1. Advertising

  3. Stan Horwitz

    Stan Horwitz Guest

    In article <>,
    Joe Smith <> wrote:

    > Stan Horwitz wrote:
    >
    > > $total = $total + $extracted_variable;
    > >
    > > Let's say $total is initialized to a value of 10 and
    > > $extracted_variable is "900".
    > >
    > > I want the result to be 910. I do not want a result of 90010.
    > >
    > > How do I do that? My current attempts all result in errors such as
    > >
    > > isn't numeric in addition (+) at

    >
    > Oh, really?
    >
    > You need to post a short but complete perl program that
    > demonstrates this problem before we can help you. I cannot
    > reproduce the problem from your description.
    >
    > use warnings;
    > $stuff=" 900xyz";
    > $extracted_variable = substr $stuff,0,8;
    > $total = 10;
    > print "Before: total='$total' extracted_variable='$extracted_variable'\n";
    > $total = $total + $extracted_variable;
    > print "After: total='$total'\n";


    Joe;

    Thanks. As it turns out, the problem wasn't in how I was adding the
    string, but how I was parsing it. I was off by a few characters in my
    parsing. As a result, my program was trying to add up non-numeric data.
    You lead me to the solution because as I was trying to assemble the code
    to follow up with your request, I realized my error.

    Thanks.
    Stan Horwitz, Feb 15, 2006
    #3
    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. Replies:
    11
    Views:
    605
    Karl Heinz Buchegger
    Apr 8, 2005
  2. John Henry

    Concatenating strings

    John Henry, Jul 1, 2006, in forum: Python
    Replies:
    1
    Views:
    293
    Steven Bethard
    Jul 1, 2006
  3. John Henry

    Concatenating strings

    John Henry, Jul 1, 2006, in forum: Python
    Replies:
    1
    Views:
    318
    Robert Kern
    Jul 1, 2006
  4. EHC

    concatenating strings

    EHC, Dec 15, 2006, in forum: Python
    Replies:
    3
    Views:
    309
    Caleb Hattingh
    Dec 16, 2006
  5. c

    Help me with Concatenating strings

    c, Sep 24, 2006, in forum: C Programming
    Replies:
    21
    Views:
    592
    Chris Torek
    Oct 15, 2006
Loading...

Share This Page