Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re: Process string as Array

by zentara (Archbishop)
on May 29, 2004 at 13:33 UTC ( [id://357473]=note: print w/replies, xml ) Need Help??


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.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others imbibing at the Monastery: (3)
As of 2024-04-24 19:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found