in reply to string to number via data dumper
Just to add to kcott’s excellent answer:
If you have a string like "$VAR1 = '-39.9999400000';\n" and want to get the number inside, you can either eval the string and use $VAR1, or else extract the number with a regular expression:
#! perl use strict; use warnings; use Data::Dumper; my @lines = <DATA>; chomp(my $line = $lines[0]); chomp(my $string = Dumper("$line")); print "Dumper returns the string \"$string\"\n"; # Method 1: eval the string my $VAR1; eval $string; # Sets $VAR1 my $m = $VAR1 + 8; print "Method 1: $m\n"; # Method 2: extract the number if ($string =~ /'([-0-9.]+)'/) { my $value = $1; my $n = $value + 8; print "Method 2: $n\n"; } __DATA__ -39.9999400000
Output:
23:27 >perl 730_SoPW.pl Dumper returns the string "$VAR1 = '-39.9999400000';" Method 1: -31.99994 Method 2: -31.99994 23:27 >
See chomp, eval, and perlretut.
Hope that helps,
| Athanasius <°(((>< contra mundum | Iustus alius egestas vitae, eros Piratica, |
|
|---|