Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: FMTYEWTK about split //

by ambrus (Abbot)
on Jan 21, 2004 at 13:21 UTC ( #322866=note: print w/ replies, xml ) Need Help??


in reply to FMTYEWTK about split //

I always used ()=$string=~/(.)/gs; to split a string to characters. Also, it's sometimes useful to iterate through the characters of a list, for which you can use the scalar-context variation: while($string=~/(.)/gs){DO SOMETHING WITH ($1)}; -- you can't do this with split, can you?

Update: added s flags, strange noone noticed that


Comment on Re: FMTYEWTK about split //
Select or Download Code
Re: Re: FMTYEWTK about split //
by Roger (Parson) on Jan 21, 2004 at 14:25 UTC
    you can't do this with split, can you?

    Sure can.
    for (split //, $string) { print "$_\n"; }
      I thought that for (split //, $string) { would cost a lot of memory if $string is long, but now I'm not sure.

        It will cost a lot of memory. Fortunately, $string is rarely large. And if it is large, the regex-iterator solution will probably be slow. You'll probably be better off with a substr solution on large strings.

        ----
        I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
        -- Schemer

        : () { :|:& };:

        Note: All code is untested, unless otherwise stated

Re^2: FMTYEWTK about split //
by particle (Vicar) on Jan 21, 2004 at 14:52 UTC

    adding to Roger's reply,

    print $_, $/ for @{[ split // => $bar ]};

    is strict and warnings friendly, as well, and twisted in it's (useless) indirection.

    ~Particle *accelerates*

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (12)
As of 2014-08-21 15:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (136 votes), past polls