Re: numbers and strings and regex?





Geoff Cox wrote:
On Mon, 30 May 2005 18:30:42 +0100, Brian McCauley <nobull@xxxxxxxx>
wrote:

my $exnum = 0+$1;
>
this work fine but I'm not clear what the

0+$1;

does?

OK, let's break if down...

0 is the number zero.

+ is the numeric addition operator.

$1 is the captured string and since the capture was (\d{2}) it will be string like '01' or '07' or '42' or something like that.

Adding zero to a number does not change it.

But Perl has automatic type coercion so adding zero to a string tells Perl to coerce the string into a number.

Hense 0+'42' is the number 42 and 0+'07' is the number 7.
.



Relevant Pages

  • Re: numbers and strings and regex?
    ... >$1 is the captured string and since the capture was it will be ... >Adding zero to a number does not change it. ... >But Perl has automatic type coercion so adding zero to a string tells ... >Perl to coerce the string into a number. ...
    (comp.lang.perl.misc)
  • Cant Compiling perl5.8.8 on FreeBSD6.2
    ... WITH_DEBUGGING=yes Build perl with debugging support. ... First let's make sure your kit is complete. ... What is the file extension used for shared libraries? ... I'll use sprintf to convert floats into a string. ...
    (comp.unix.bsd.freebsd.misc)
  • Re: use of DBI; I am getting multiple error messages mixed in with ?the correct output.
    ... But I'm not talking about C++ or Java or SQL. ... This is a perl newsgroup. ... to means by a "defined null string", as opposed to an undefined value. ... And where exactly are you getting the idea that the empty string is a ...
    (comp.lang.perl.misc)
  • Re: Perl Strings vs FileHandle
    ... Just wanted to run this through Perl gurus to see if fit is ... testing it every time through the loop. ... The comparison test to the string '1' is superfluous. ... Shared Hosting, Reseller Hosting, Dedicated & Semi-Dedicated servers ...
    (comp.lang.perl.misc)
  • Re: Working with Source Code to Insert Copyright Statements as a Header
    ... Paul Lalli wrote: ... Where could I go to find some samples of PERL code that do the ... ascii english text, etc etc etc) at the top of the file, from a string: ... `perldoc Tie::File` at the command line to read its documentation. ...
    (comp.lang.perl.misc)