Here's my take on it:

Let's say you've got a string like this:

my $string = "12 word 100,000 word 20,300 word word 180";

And let's say that you want to split that string on whitespace into an array. You then wish to be able to perform mathematical operations on any of the numeric values, including the ones with commas in them.

You also want the original string to be left in tact, but I get the impression it's ok for the split out values to be modified to facilitate coersion of numeric strings into numeric values.

Ok, here goes...

use strict; use warnings; my $string = "12 word 100,000 word 20,300 word word 180"; my @array = split /\s+/, $string; # Perform your split. $_ =~ tr/,//d for @array; # Tidy up, removing commas. $array[2] /= 10; # Do some math as a test. print "$array[2]\n"; # Print the results of the # math which we just performed # on a value which had previously # been a string.


Dave


In reply to Re: changing a string to a numeric variable by davido
in thread 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.