[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [MacPerl] FNG



kevin dowd wrote:
> 
> 
> I'm testing out basic  PERL functions and  I do the following three lines - copied exactly out of my new PERL book, apart from the variable of course.
> 
> $myEmail =  'dowd%40ndirect.co.uk';
> $myEmail = ~ s/%40/@/g;
> print "$myEmail";
> 
> All I get back is 4294967295.
> 
> I want to replace the %40 with a @.
> 
> But I'd like to know what the 4294967295 is - for next time
> 

Why 4294967295 is (2**32)-1, of course! ;) That's two to the
thirty-second power, minus one. Why did you get it? Interesting
question. 

in line 3 you have = ~ which is *not* the same as =~. You are
accidentally using the bitwise negation operator "~". A chain of events
occurs:

 0) There is no string specified to search (because =~ was not used) so
the substitution tries to operate on nothing
 1) the substitution fails, returning a 0 (the number of substitutions)
 2) you assign $myEmail the value of the bitwise negation of 0,
represented as a 32 bit integer
 3) you print that value, which happens to be 4294967295

Try this to get the result you want:
#!perl -w
$myEmail =  'dowd%40ndirect.co.uk';
$myEmail =~ s/%40/@/g;   #remove the space here
print "$myEmail";

For fun, try this, which is essentially what you did earlier:
#!perl -w
$myEmail = 0;
print ~ $myEmail;

Hope that wasn't too gruesome. You were almost there, just one little
typo. To paraphrase Chris Nandor's recent notes on this list: always use
the -w switch and be precise when typing.

Geoff

***** Want to unsubscribe from this list?
***** Send mail with body "unsubscribe" to mac-perl-request@iis.ee.ethz.ch