Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Re: string to number via data dumper

by Happy-the-monk (Canon)
on Sep 28, 2013 at 12:10 UTC ( [id://1056121]=note: print w/replies, xml ) Need Help??


in reply to string to number via data dumper

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)

Replies are listed 'Best First'.
Re^2: string to number via data dumper
by Anonymous Monk on Sep 28, 2013 at 12:14 UTC

    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).
        "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)."

        I know you've only been using Perl for a short amount of time, but I don't think you've really got an understanding of the basics. Try reading "perlintro -- a brief introduction and overview of Perl": you need a solid foundation on which to build further knowledge.

        With regard to your "$val+8" question, this code should explain why it's not working:

        #!/usr/bin/env perl -l use strict; use warnings; my @items = (-39.9999400000); my $val = $items[-1]; print $val; my $n = "$val+8"; print $n; my $x = $val+8; print $x;

        Output:

        -39.99994 -39.99994+8 -31.99994

        -- Ken

        Ken said it all, but just in case you have trouble understanding his example, just change:

        my $n= "$val+8"; print $n;

        to:

        my $n= $val+8; print $n;

        In the first code snippet above, you are constructing a string composed of the concatenation of your value (in string form) and the "+8" string. In other words, you are just doing string manipulation, no arithmetics. The second code snippet is actually adding the number 8 to your value. Now, if you understood the difference, go back to Ken's example.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1056121]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (5)
As of 2024-04-18 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found