Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
The atoi() function is a truly ancient thing. People were hunting dinner with spears when this thing first appeared, ...way before C. In C if you "abuse" this, there aren't any warnings. It is the user's responsibility to check the input values before calling atoi(). Some cases and normal ways to check for this are shown below.

#include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char *digits="0123456789"; char x[] = "2abc"; int result = atoi(x); printf ("case a) string %s is %d as decimal\n",x,result); if (strlen(x) == 0 || (strspn(x,digits) != strlen(x))) printf ("case b) %s is NOT a simple positive integer!\n" " there are either no digits or non-digits\n", x); char y[] = ""; int result_y = atoi(y); printf ("case c) a null string results in %d\n",result_y); return 0; } /* prints: case a) string 2abc is 2 as decimal case b) 2abc is NOT a simple positive integer! there are either no digits or non-digits case c) a null string results in 0 */
Perl is more generous and gives warnings if they are enabled. This will give a warning:
#!/usr/bin/perl -w use strict; my $a = "23B"; $a +=0; print $a;
You can check yourself if \D exists in string or if null string. Otherwise Perl will do the "best that it can".

Also of note is that in Perl, everything is a string a string that looks like a number is still a string until used in a numeric context. Consider this:

my $x = "00012"; print "$x\n"; $x+=0; print "$x\n"; ##prints #00012 #12
This is a "trick" that I sometimes use to delete leading zeroes.

In reply to Re^2: Numification of strings by Marshall
in thread Numification of strings by LanX

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?

    What's my password?
    Create A New User
    and all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others imbibing at the Monastery: (7)
    As of 2018-06-20 19:40 GMT
    Find Nodes?
      Voting Booth?
      Should cpanminus be part of the standard Perl release?

      Results (117 votes). Check out past polls.