Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-25 13:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found