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,


In reply to Re: string to number via data dumper by Athanasius
in thread string to number via data dumper by semipro

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.