Re: Padding a length of string
"Spyder N'Sally" <[email protected]>
| Newsgroups | gmane.comp.lang.perl.fun |
|---|---|
| Message-ID | <[email protected]> |
I'd avoid the looping construct if I were you, unless you're writing in c.
her you go minus the c-syle loops...
#!/usr/bin/perl -wd
my $padLength = 10;
my $padChar = '!';
$_ = 'catdog';
# this line does ALL of the work...
$_ .= $padChar x ( $padLength - length() );
print "$_\n";
Brian Morgan wrote:
>Hello all,
>Here is something that I have been playing with, and I am wondering if it
>can be shortened at all.
>
>The script is given a string that is shorter then a MAX length as well as a
>special character. It
>then adds onto the end of the string the special character until the length
>of the string is equal
>to MAX.
>
>Here is my solution as an example of what I am talking about:
>#BEGIN EXAMPLE
>
>$max = $ARGV[0];
>$key = $ARGV[1];
>$pad_string = $ARGV[2];
>@key_chars = split(//, $key);
>$count = $#key_chars + 1;
>while ($count <= $max){
> $key .= $pad_string;
> $count++;
>}
>print $key;
>
>#END EXAMPLE
>
>So can anyone out there compress this down? I can't wait to see the
>results.
>
>Thanks,
>Brian
>
>
_________________________________________________________________
FREE pop-up blocking with the new MSN Toolbar get it now!
http://toolbar.msn.com/go/onm00200415ave/direct/01/