bdimych has asked for the wisdom of the Perl Monks concerning the following question:
That is what I've written
1) - ok on empty string, but on not empty string produce unnecessary blank substring
2) - ok on not empty string, but empty string is not paddedmy $x = ""; $x=~s/(.{0,3})/"'$1".(' 'x(3-length($1)))."'\n"/eg; print $x --- ' ' my $x = "1234567890"; $x=~s/(.{0,3})/"'$1".(' 'x(3-length($1)))."'\n"/eg; print $x --- '123' '456' '789' '0 ' ' '
any advice?my $x = ""; $x=~s/(.{1,3})/"'$1".(' 'x(3-length($1)))."'\n"/eg; print $x --- ___prints_nothing___ my $x = "1234567890"; $x=~s/(.{1,3})/"'$1".(' 'x(3-length($1)))."'\n"/eg; print $x --- '123' '456' '789' '0 '
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: How to split_and_pad string to n-th chars pieces with one regular expression?
by BrowserUk (Patriarch) on Nov 30, 2007 at 14:27 UTC | |
|
Re: How to split_and_pad string to n-th chars pieces with one regular expression?
by Limbic~Region (Chancellor) on Nov 30, 2007 at 14:19 UTC | |
|
Re: How to split_and_pad string to n-th chars pieces with one regular expression?
by mwah (Hermit) on Nov 30, 2007 at 14:40 UTC | |
|
Re: How to split_and_pad string to n-th chars pieces with one regular expression?
by bdimych (Monk) on Nov 30, 2007 at 15:00 UTC |