G'day semipro,

You really need to show your code (i.e. how you're using the value in a calculation), the output you get as well as any error or warning messages (these need to be exactly as you see them and marked up within <code>...</code> tags). Please read the "How do I post a question effectively?" guidelines for full details regarding this.

I suspect the message you're getting is something like:

Argument "$VAR1 = '-39.99994';\n" isn't numeric ...

The part that isn't numeric is:

$VAR1 = '-39.99994';\n

This is the string that Dumper($items[-1]) would be returning: '-39.99994' is just part of that string.

I think you should spend some time reading the Data::Dumper documentation. I suspect you haven't quite understood what it does. A more usual usage would be something like:

print Dumper \%some_hash;

There'll be many examples in the monastery (use Super Search). Here's a couple from recent nodes I've written: hashref data structure dump and arrayref data structure dump.

Perl will happily convert numbers between string and numeric contexts. For example, this code:

#!/usr/bin/env perl -l use strict; use warnings; my $val = '-39.9999400000'; print $val; print 2.5 * $val; my $val2 = -39.9999400000; print $val2; print "Number in a string >>> $val2 <<<";

produces this output:

-39.9999400000 -99.99985 -39.99994 Number in a string >>> -39.99994 <<<

You'll notice that trailing zeros were truncated. You can control the format with sprintf.

You can also force the context yourself: 0+$x (numeric); ''.$x (string); !!$x (boolean).

-- Ken


In reply to Re: string to number via data dumper by kcott
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.