padr()

T

toylet

Can this be done with an ounce with regular expression only?

print padr("99",5,"0"); #00099

sub padr {
($char, $maxlen, $filler) = @_;
$x=$maxlen-length($char);
return $filler x $x . $char;
}
 
J

Jeff 'japhy' Pinyan

Can this be done with an ounce with regular expression only?

Hrm? Why would you want to do this with a regex? Anyway, there's already
a function that can do this, sprintf() (and printf()).
print padr("99",5,"0"); #00099

printf "%05d", 99; # 00099
$formatted = sprintf "%05d", 99;

And if you want to use a variable for the width, you can do so:

printf "%0*d", 6, 142; # 000142

Read 'perldoc -f printf' and 'perldoc -f sprintf'.
 
U

Uri Guttman

"t" == toylet <toylet_at_mail.hongkong.com> writes:

t> Can this be done with an ounce with regular expression only?
t> print padr("99",5,"0"); #00099

huh??


t> sub padr {
t> ($char, $maxlen, $filler) = @_;
t> $x=$maxlen-length($char);
t> return $filler x $x . $char;
t> }

please learn some basic perl. you are using GLOBAL variables for args
there. use strict or die!!

why would you want to use a regex? any workable regex (and it can be
done but i won't show you) will be more complex looking than your sub
call. and don't say but the body of the sub is complex. the key is what
the call looks like as you use the sub to HIDE the complexity. so again,
why do you want to do this? and write proper perl code. you are going to
get slammed for that sub by all who followup.

uri
 
T

toylet

done but i won't show you) will be more complex looking than your sub
call. and don't say but the body of the sub is complex. the key is what

No, I won't.
the call looks like as you use the sub to HIDE the complexity. so again,
why do you want to do this? and write proper perl code. you are going to
get slammed for that sub by all who followup.

Beause my focus was/is not in programming style, but the possiblity of
doing padr() in other ways.
 
U

Uri Guttman

t> No, I won't.

t> Beause my focus was/is not in programming style, but the possiblity of
t> doing padr() in other ways.

programming style includes proper coding. not using strict or my is not
good even in examples as it leads to newbies following that style. all
you had to do was my the args and that isn't much work. it shows you
understand and respect perl.

uri
 
T

toylet

programming style includes proper coding. not using strict or my is not
good even in examples as it leads to newbies following that style. all
you had to do was my the args and that isn't much work. it shows you
understand and respect perl.

I agree one should always use software engineering techniques. But
that's not the focus of my question.
 
U

Uri Guttman

t> I agree one should always use software engineering techniques. But
t> that's not the focus of my question.

then you don't understand. and that is not good for you at all.

uri
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,146
Messages
2,570,832
Members
47,374
Latest member
EmeliaBryc

Latest Threads

Top