Greetings, Monks,

Suppose I have a line with both text and numbers in it. Maybe it looks like this:

word 15 word word 20

If I split the line this way:

($a,$b,$c,$d,$e) = split

I find I can use $b and $e as numbers-- do math on them and stuff.

But--

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:

@line = split

And now, though $line[1] and $line[4] 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[1] 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.

Thanks,

NovMonk


In reply to changing a string to a numeric variable by NovMonk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.