Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

How can I split a string into chunks of 4 characters

by PetreAdi (Acolyte)
on Dec 06, 2013 at 16:23 UTC ( [id://1066004]=perlquestion: print w/replies, xml ) Need Help??

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

For example, string "01234abc" would become array ("0123", "1234","234a","34ab","4abc")

I tried @array = $string =~ /(.{1,4})/g; but it doesn't seem to work.

Thank you.

  • Comment on How can I split a string into chunks of 4 characters

Replies are listed 'Best First'.
Re: How can I split a string into chunks of 4 characters
by Athanasius (Archbishop) on Dec 06, 2013 at 16:31 UTC

    Use a positive look-ahead assertion, like so:

    2:28 >perl -MData::Dump -we "my $s = '01234abc'; my @a = $s =~ /(?=(. +{4}))/g; dd @a;" ("0123", 1234, "234a", "34ab", "4abc") 2:29 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: How can I split a string into chunks of 4 characters
by davido (Cardinal) on Dec 06, 2013 at 16:54 UTC

    my @array = map { substr $string, $_, 4 } 0 .. length( $string ) +- 4;

    If the string is really long, use a $position variable and a while loop so that internally the big 0 .. length( $string ) - 4 list doesn't need to be built and stored:

    my $pos = 0; while( $pos <= length( $string ) - 4 ) { push @array, substr( $string, $pos++, 4 ); }

    Dave

Re: How can I split a string into chunks of 4 characters
by BrowserUk (Patriarch) on Dec 06, 2013 at 21:26 UTC

    $str = '0123456789abcdef';; print for unpack '(a4X3)' . (length($str)-3), $str;; 0123 1234 2345 3456 4567 5678 6789 789a 89ab 9abc abcd bcde cdef

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
Re: How can I split a string into chunks of 4 characters
by Random_Walk (Prior) on Dec 06, 2013 at 16:31 UTC
    use strict; use warnings; use 5.010; my $len = 4; my $string = "01234abc"; for (0 .. (length $string - $len) ) { say substr $string, $_, $len; } # or in an array my @chunks = map {substr $string, $_, $len} (0 .. (length $string - $l +en) ); say join "<>", @chunks;

    Cheers,
    R.

    Pereant, qui ante nos nostra dixerunt!
Re: How can I split a string into chunks of 4 characters
by oiskuu (Hermit) on Dec 07, 2013 at 12:01 UTC
    A bit arcane, but ought to work, provided length($string) >= 3:
    @array = unpack 'x*X3 .@0/(a4X3)', $string;
    I'd recommend Athanasius's solution though:
    @array = $string =~ m/(?=(.{4}))/g;
Re: How can I split a string into chunks of 4 characters
by kcott (Archbishop) on Dec 07, 2013 at 14:35 UTC

    G'day PetreAdi,

    Here's another way to do it.

    #!/usr/bin/env perl -l use strict; use warnings; my $string = '01234abc'; my @chars = split // => $string; my @chunks; push @chunks, join '' => @chars[$_ .. $_ + 3] for 0 .. length($string) + - 4; print "@chunks";

    Output:

    0123 1234 234a 34ab 4abc

    -- Ken

Re: How can I split a string into chunks of 4 characters
by Lennotoecom (Pilgrim) on Dec 06, 2013 at 18:06 UTC
    $_ = '01234567890'; push @a, $1 while s/(.{1,4})//;
    update
    didn't read the task properly. my bad sorry
    new solution:
    $_ = '01234abc'; $_ = $2.$', push @a, $& while /(.)(.{3})/;

      "push @a, $1 while s/(.{1,4})//;" produces the following: "0123 4abc", which is not the desired result.


      Dave

        i will cite the author
        "For example, string "01234abc" would become array 0123 4abc"
        it is exactly what my script does
        you sir, I suppose, didn't read the task

        got mistaken. im sorry

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1066004]
Approved by toolic
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-16 06:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found