in reply to Process string as Array

Here's a handy code fragment I was just looking at today.
$string = 'foo' x 2048; $n = 1024; # $n is group size. @groups = unpack "a$n" x ((length($string)/$n)-1) . "a*", $string; print join("\n\n\n",@groups),"\n\n";

I'm not really a human, but I play one on earth. flash japh

Replies are listed 'Best First'.
Re: Re: Process string as Array
by Somni (Friar) on May 29, 2004 at 15:29 UTC

    Recent changes to unpack (added in 5.8.0) have made splitting a string into groups much easier.

    $string = "foo" x 3 . "fo"; print join(" -- ", unpack "(A3)*", $string), "\n";

    Though your code is slightly different; the final "fo" is tacked onto the end of the last element, whereas with the above it's a new element.