Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

Split string into equal parts

by kalyanrajsista (Scribe)
on May 28, 2010 at 16:39 UTC ( #842072=perlquestion: print w/replies, xml ) Need Help??
kalyanrajsista has asked for the wisdom of the Perl Monks concerning the following question:

Hello Monks

I've tried the following code in splitting a string into 4 equal parts separated by a space. I've looking for a good method to refine my code or suggest a good way to do....

use strict; my $tempStr = 'BE65826000483296'; my $tempStr1; while ( $tempStr . length ) { $tempStr1 .= " " . substr( $tempStr, 0, 4 ); $tempStr = substr( $tempStr, 4 ); } $tempStr1 =~ s/^\s//isg; print $tempStr1;

Many Thanks

Replies are listed 'Best First'.
Re: Split string into equal parts
by BrowserUk (Pope) on May 28, 2010 at 16:44 UTC

    You're working way too hard:

    $tempStr = 'BE65826000483296'; print join ' ', unpack '(A4)*', $tempStr; BE65 8260 0048 3296
      Thanks BrowserUK, Your one-liner solves my problem
Re: Split string into equal parts
by ikegami (Pope) on May 28, 2010 at 16:48 UTC

      For those still stuck in 5.8-dom, it's possible to do this substitution without use of the  \K special escape of 5.10:

      >perl -wMstrict -le "for (@ARGV) { (my $sp = $_) =~ s{ (.{4}) (?= .) }{$1 }xmsg; print qq{'$_' -> '$sp'}; } " "" A ABC ABCD ABCDa ABCDabc ABCDabcd ABCDabcdE '' -> '' 'A' -> 'A' 'ABC' -> 'ABC' 'ABCD' -> 'ABCD' 'ABCDa' -> 'ABCD a' 'ABCDabc' -> 'ABCD abc' 'ABCDabcd' -> 'ABCD abcd' 'ABCDabcdE' -> 'ABCD abcd E'

      This approach is likely less efficient due to use of a capture. Is it possible to do this operation in a pre-5.10 solo substitution (i.e., no  m//  unpack  join   etc.) without a capture and in a fairly simple way (for some definition of 'fairly simple')?

      Thank you ikegami, You're one-liner is awe-some but unfortunately I'm still using Perl 5.8.8 and thus unable to use your solution. Just curious to know any other solution which works in any version of Perl
Re: Split string into equal parts
by johngg (Abbot) on May 28, 2010 at 20:44 UTC

    Another way would be to use substr to insert spaces working back from the right-hand end of the string.

    $ perl -E ' > $str = q{abcdefghijklmn}; > say $str; > substr $str, $_, 0, q{ } for > grep { ! ( $_ % 4 ) } reverse 4 .. length( $str ) - 1; > say $str;' abcdefghijklmn abcd efgh ijkl mn $

    I hope this is of interest.



Re: Split string into equal parts
by proceng (Scribe) on May 28, 2010 at 20:24 UTC
    splitting a string into 4 equal parts separated by a space
    You would need to get the length of the string and divide it by 4. However, your (not so) edge case is where you do not have a length that is a multiple of 4 (think wep keys).
    How do you want to handle that?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://842072]
Approved by Old_Gray_Bear
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2017-06-24 13:11 GMT
Find Nodes?
    Voting Booth?
    How many monitors do you use while coding?

    Results (557 votes). Check out past polls.