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

Re: Zero Padding

by mkmcconn (Chaplain)
on Feb 03, 2001 at 22:40 UTC ( #56238=note: print w/replies, xml ) Need Help??

in reply to Zero Padding

Variation on a theme, with a twist that I don't think has been mentioned. I've tried to do my homework, and nothing stood out that would break this. What do you think?

#!/usr/bin/perl -wl use strict; my @numberarray = qw(1 120 13); lengthy(@numberarray); sub lengthy { s{ ( ^ ) } { ( "0" x (6-length $_) ) }xe and print for @_; } __END__

Or, for the commandline:
perl -wle ' s/^/("0"x(6-length))/xe and print for @ARGV;' 133 121 10 13
Or, to make sure that it doesn't zero-pad alphabetical input
perl -wle ' s{(^)(\d+$)}{("0"x(6-length).$2)}xe and print for @ARGV;' 1 fish 002 fish 333 blue fish
And, to print all the arguments, but zero-padding only the numerical arguments:
perl -wle ' s{(^)(\d+$)}{("0"x(6-length).$2)}xe and print or print for @ARGV;' 1 fish 002 fish 333 blue fish

(Sir...put your hands up and please, step away from the keyboard)
One more.. to pad words with spaces, instead of zeros.

perl -wle ' s/^(\d+)/("0"x(6-length).$1)/xe and print or s/^(\w)/("\x20"x(6-length).$1)/xe and print for @ARGV;' 133 121 10 + 13 red

(I'm gratified by how much better it is than this a few short weeks later. Thanks Perl Monks.).

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2023-05-28 06:00 GMT
Find Nodes?
    Voting Booth?

    No recent polls found