Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

string to number via data dumper

by semipro (Novice)
on Sep 28, 2013 at 11:52 UTC ( #1056118=perlquestion: print w/ replies, xml ) Need Help??
semipro has asked for the wisdom of the Perl Monks concerning the following question:

Hi there Perlmonks! while gathering experience I tumbled over the Data Dumper package which just provides some interesting features. However while giving me single values of a certain line from a dat document, it can only return strings values. But unfortunately I need the number as a numerical value to further process it. Thats where I am struggeling. Here the code:
#!/usr/bin/perl -w use strict; my $resultnow='C:/Users/user/Desktop/solution.dat'; open (FILE, '<', $resultnow) or die "$resultnow File not found : $!"; my @lines = <FILE>; my $valuenow= $lines[9]; my @items=split' ',$valuenow; use Data::Dumper; #print Dumper(@items); my $val=Dumper($items[-1]); print $val; close (FILE);
In this case the value he is extracting is: -39.9999400000. but he marks it as a string '-39.9999400000'. But I would like to insert it in an equation to calculate something. So I need somehow a conversion or an alternative. Thank you again very much for your attention and help! Best regards

Comment on string to number via data dumper
Download Code
Re: string to number via data dumper (strings are number, perlnumber)
by Anonymous Monk on Sep 28, 2013 at 12:09 UTC
Re: string to number via data dumper
by Happy-the-monk (Monsignor) on Sep 28, 2013 at 12:10 UTC

    Fortunately, Perl will convert your string back to a number whenever you treat it as one.
    like say $stuff + 0 ; # print $stuff + 0 , "\n" ; # on older perl versions.

    I also believe Data::Dumper did not convert your number to a string but added quotes for clarity.

    Cheers, Sören

    Créateur des bugs mobiles - let loose once, run everywhere.
    (hooked on the Perl Programming language)

      I also believe Data::Dumper did not convert your number to a string but added quotes for clarity.

      Yep, it simply quoted the strings -- a string is not a number until its treated like a number :)

      $ perl -MData::Dump -e " dd '+11', 0+ '+11' " ("+11", 11)
        Hi there, thank you. While treating the output as number so eg:
        #!/usr/bin/perl -w use strict; my $resultnow='C:/Users/user/Desktop/solution.dat'; open (FILE, '<', $resultnow) or die "$resultnow File not found : $!"; my @lines = <FILE>; my $valuenow= $lines[9]; my @items=split' ',$valuenow; use Data::Dumper; #print Dumper(@items); my $val=Dumper("$items[-1]"); my $n= "$val+8"; print $n; close (FILE);
        here I tryed to make a sum. with "my $n= "$val+8";", so that I can obtain a vlue like -31.9999400000 (instead of '-39.9999400000'+8). But unfortunately that does not work. So how can I solve this problem? (that was the reason why I thought it is a string value).
Re: string to number via data dumper
by kcott (Abbot) on Sep 28, 2013 at 13:18 UTC

    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

Re: string to number via data dumper
by Athanasius (Monsignor) on Sep 28, 2013 at 13:32 UTC

    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,

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1056118]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2014-08-30 06:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (291 votes), past polls