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
mkmcconn
(I'm gratified by how much better it is than this a few short weeks later. Thanks Perl Monks.).
|
---|
In Section
Seekers of Perl Wisdom