Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

How to split_and_pad string to n-th chars pieces with one regular expression?

by bdimych (Monk)
on Nov 30, 2007 at 14:01 UTC ( [id://654108]=perlquestion: print w/replies, xml ) Need Help??

bdimych has asked for the wisdom of the Perl Monks concerning the following question:

Hi All. I need to split the string to a several substrings of the same length, the last substring should be blank padded so that it's length also would be the same. And - that's the problem - the string may be empty also.

That is what I've written
1) - ok on empty string, but on not empty string produce unnecessary blank substring

my $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 ' ' '
2) - ok on not empty string, but empty string is not padded
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 '
any advice?

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
    bdimych,
    any advice?

    Yes - stop trying to do this in a single regular expression. While I am sure there will be a dozen correct and working solutions provided, I doubt any will be self-documenting. I also suspect anyone maintaining your code will take pause when they come across it.

    Why not come up with a split_by_n() function where you can pass in a string and a length and get back what you want. Heck, you could then have optional parameters for what to do with empty strings, padding or not, etc.

    Cheers - L~R

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

    Maybe I misunderstood the problem, but I'd try:

    $txt =~ s/(.{1,3})/sprintf"%03s\n",$1/ge;

    Update: missed the "empty string" part:

    ... $txt =~ s/(.{1,3}|^$)/sprintf"'%3s'\n",$1/ge; print $txt; ...

    Regards

    mwa

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
    many thanks, it is solved,
    If combine BrowserUk's and mwah's solutions, then it works fine
    $x=~s/(.{1,3}|^$)\n?/sprintf"'%-3s'\n",$1/eg;
    it works even on multiline strings
    ---
    and even multiline with emptyline mixed together
    $x=~s/(.{1,3}|^$)\n?/sprintf"'%-3s'\n",$1/meg;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://654108]
Approved by moritz
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (4)
As of 2024-04-19 02:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found