in reply to string to number via data dumper

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