CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner: Re: [bu420] Print numbers between 1 and 9999: Edit Log



Chris Charley
User

Jan 24, 2016, 10:04 AM


Views: 3839
Re: [bu420] Print numbers between 1 and 9999

if user enter 0001/001/01...it will not give proper output

When the user enters a number that begins with '0', it makes '$size ' longer. That is, '0014' will have a size of '4' and your code processes in the '$size == 4' section. You really want to strip those 2 leading zeros so that your code will process it in the '$size == 2' section.

You could modify your code to do this by using the substitution operator. (Also, you do not handle the case of zero). The code to do this would be:


Code
($value = $ARGV[0]) =~ s/^0+//; #strip leading zeros 
print "zero\n" and exit if $value eq '';

... rest of your code unchanged


Update: After reading your post, I realized you didn't need to check for zero. But it wouldn't hurt to leave the check in even if you don't enter a zero on the command line.

Update 2A better solution than the one above would be to add '0' to $ARGV[0]. It is at first a string but when you add to it, it becomes a number (and drops the leading zeros).


Code
$value = $ARGV[0] + 0; 
print 'zero' and exit if $value == 0;



(This post was edited by Chris Charley on Jan 24, 2016, 10:17 PM)


Edit Log:
Post edited by Chris Charley (User) on Jan 24, 2016, 1:05 PM
Post edited by Chris Charley (User) on Jan 24, 2016, 1:07 PM
Post edited by Chris Charley (User) on Jan 24, 2016, 10:17 PM


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives