in reply to changing a string to a numeric variable
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
|
|---|