Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Subtracting Stringified Negative Numbers

by Zaxo (Archbishop)
on Jun 22, 2007 at 04:23 UTC ( [id://622709]=note: print w/replies, xml ) Need Help??


in reply to Subtracting Stringified Negative Numbers

It works for me,

$ perl -w -e'my$n1="-933";my$n2="-1039";my$dif=$n1-$n2;print $dif,$/' 106 $
Your error messages say directly that you have more characters than minus, plus and digits in your argument variables. The presence of "^\" at the start of each string is causing the problem, making the strings not look like numbers.

Figure out how the spurious characters are getting in and you'll find the solution.

After Compline,
Zaxo

Replies are listed 'Best First'.
Re^2: Subtracting Stringified Negative Numbers (Data::Dumber)
by tye (Sage) on Jun 22, 2007 at 05:27 UTC

    And this reminds me of another reason why I don't use Data::Dumper. It defaults to being "efficient" and so doesn't actually 'dump' the variable contents, in an effort to avoid the arduous task of escaping all those weird characters. You have to go out of your way to tell Data::Dumper to "useqq" for it to actually dump strings in a fool-proof manner.

    > perl -del # ... DB<4> print Dumper("\c\\-10") $VAR1 = '-10'; DB<5> print Data::Dumper->new(["\c\\-10"])->Useqq(1)->Dump() $VAR1 = "\34-10"; DB<6>

    The unescaped CTRL-\ removed from the above output as how it would be displayed would depend on several things and it didn't even remain a CTRL-\ after being copy-n-pasted into the browser and converted to Latin-1 etc. anyway.

    Update: Oh, I thought Zaxo had noted what "^\" represents but I see he didn't so, to be very clear, "^\" is how perl is safely dumping a single CTRL-\ ("\c\\") character in that warning, which is why my example code looks like it does. No, I didn't realize this when first viewing the warning.

    - tye        

      You are right, tye. Following your suggestion, and by doing this:
      my $some_string = "0,-933"; $Data::Dumper::Useqq = 1; print Dumper ($some_string) ; my ($id1,$n1) = split(",",$some_string); #then do things with $n1 and $n2.
      It gives:
      $VAR1 = "0,\34-933";
      So my questions are:
      • How can this \34 comes, in the first place? Can we avoid that?
        My development is always under Linux/Unix platform. And those variable values are read from a file.
      • How to remove this \34 character from $VAR?

      Regards,
      Edward

        You're still posting snippets of what you think is going on, instead of snippets of real code. For example, your latest snippet simply doesn't do what you are suggesting it does:

        use Data::Dumper; $Data::Dumper::Useqq=1; my $some_string = "0,-933"; print Dumper $some_string; $VAR1 = "0,-933";
        So my questions are:
        • How can this \34 comes, in the first place? Can we avoid that?
        • My development is always under Linux/Unix platform. And those variable values are read from a file.
        • How to remove this \34 character from $VAR?

        The only conclusion we can draw from what you've posted so far is that the file you are reading these values from contains these \x1c (octal \34) characters.

        Try dumping the file using the od command:

        od -b \path\to\the\file # or od -tx1 \path\to\the\file

        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (5)
As of 2024-03-29 15:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found