Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Can you make it nicer?

by oiskuu (Pilgrim)
on Nov 19, 2013 at 13:45 UTC ( #1063331=note: print w/ replies, xml ) Need Help??


in reply to Can you make it nicer?

Using sprintf to stringify a number, you may want to check its range to avoid negatives or overflow surprises:

my $id = shift; # disallow ID 0; $id > 0 && $id < 1e9 or return q(); $id = sprintf '%0*lu', ($id < 1e6 ? 6 : 9), $id; ...
Or, you could let perl stringify the number, and munge it from there.
sub id2path { my $id = shift; # allow ID 0; disallow 12.34 $id =~ m/^\d{1,9}$/ or return q(); my $n = length($id) > 6 ? 3 : 2; $id = sprintf '%0*s', 3 * $n, $id; $_ = "/$_/" for substr($id, $n, $n); # or: substr($id, $_, 0, '/') for -2*$n, -$n; return $id; }
Interestingly, perl sprintf allows zero-padding of %s! Is this a documented feature?


Comment on Re: Can you make it nicer?
Select or Download Code

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1063331]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (5)
As of 2014-09-20 05:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (153 votes), past polls