|No such thing as a small change|
changing a string to a numeric variableby NovMonk (Chaplain)
|on Mar 18, 2004 at 21:43 UTC||Need Help??|
NovMonk has asked for the
wisdom of the Perl Monks concerning the following question:
Suppose I have a line with both text and numbers in it. Maybe it looks like this:
If I split the line this way:
I find I can use $b and $e as numbers-- do math on them and stuff.
I have a slightly messier line. Instead of 15 I have 15,000 and instead of 20, it's 20,000. I assign it as an array thus:
And now, though $line and $line print out as 15,000 and 20,000, when I try to do any math on them I get an error, that 15,000 or 20,000 isn't a numeric in line whatever.
The question I was going to ask is-- is this something to do with assigning this as an array instead of scalars, but I just stumbled across that answer. I see it's the commas in the variables in the second case causing the problem.
So, new question-- how can I best manipulate $line etc to get rid of the comma? I don't want to change the line I'm splitting because I'm using it later and the comma needs to be there then. I'm having trouble knowing how to think about the string once it's been assigned to a variable. Or, whether I'm even thinking about this the right way at all. ;)
ps this isn't homework. I'm teaching myself trying to solve some simple problems at work, and until I crashed into this I was doing pretty well. We all gotta start somewhere.