473,473 Members | 1,520 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

adding strings, not concatenating them

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.
Feb 15 '06 #1
2 6664
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
Feb 15 '06 #2
In article <QZ********************@comcast.com>,
Joe Smith <jo*@inwap.com> 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.
Feb 15 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Bryant | last post by:
(N.B. - this may be a well-known thing, but it was new to me). I was reading a script written by someone else, and noticed a coding style difference - they always concatenated variables into...
20
by: Trevor | last post by:
I have a couple of questions regarding C# strings. 1) Sometimes I see a string in someone else's code like: string foo = @"bar"; What does the '@' do for you? 2) Is there a performance...
4
by: Juan | last post by:
Does any one know if there are reported bugs when concatenating strings? When debugging each variable has the correct value but when I try to concatenate them some values are missing (I can´t see...
6
by: charliewest | last post by:
Hi. I've a rather newbie question: I'm concatenating several strings, adding "\n" where necessary, and setting the final string to the text property of a multi-line textBox control. However,...
10
by: Nick Z. | last post by:
I do some logging like so: Log.Error("The song was not found. Artist - " + artist + ", Title - " + title + "\n" + exception.ToString()); Now I know that building strings in that way is...
73
by: Rigga | last post by:
Hi all, I am wondering why string's are not true objects?.... Let me explain... If i write the code Dim x1 as String = "veg" Dim x2 as String = "veg" If x1 = x2 then
7
by: Marcel | last post by:
I am C++ beginner. When i start building very basic things like hello world programs i am very attired by the <string> library so i put all text things in strings. Is that good programmers...
14
by: gorio | last post by:
I have a MySQL/PHP based page with a number of date fields that I would like the user to be able to run a script on to update the fields and then submit them for server processing. All my...
21
by: c | last post by:
Hi everybody. I'm working on converting a program wriiten on perl to C, and facing a problem with concatenate strings. Now here is a small program that descripe the problem, if you help me to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.