Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re: Integer regex, different results in windows and mac - I just need regex help

by haukex (Abbot)
on Oct 19, 2017 at 08:16 UTC ( #1201659=note: print w/replies, xml ) Need Help??


in reply to Integer regex, different results in windows and mac - I just need regex help

Just to confirm what the others have said, if you save your file with CRLF line endings on a system that uses LF line endings (*NIX, including Mac OS X) and run it, it gives the wrong results. If you then put binmode DATA, ':crlf'; just before the loop, it works again. :crlf is the default on Windows, see open and binmode, and it causes the CRLF line endings to be converted to LF, so that chomp works properly. Without :crlf, what is happening is that $num contains a trailing CRLF, but chomp only removes the LF, leaving the strings looking like "19\r", which you can see yourself if you use Data::Dump or Data::Dumper with $Data::Dumper::Useqq=1; - see also the Basic debugging checklist. The best solution IMO is to convert the file to LF line endings, for example using fromdos from the Tofrodos package, and also many text editors support selecting the line endings in the "Save As" dialog.

Also, regarding your regexes, I just wanted to point out Regexp::Common::number, which you might find useful, and also that the regex /^[0].{0}/ looks a little suspicious: .{0} doesn't really do anything, and rewriting the regex as /^0/ shows that it will accept any string beginning with a 0, including stuff like "0 but true", is that really what you want?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1201659]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2018-07-16 04:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (332 votes). Check out past polls.

    Notices?