Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

Re: Split on every second character

by ikegami (Patriarch)
on Feb 12, 2010 at 23:06 UTC ( [id://822951]=note: print w/replies, xml ) Need Help??


in reply to Split on every second character

When you use split, the pattern must match what separates what you want. In this case, the separator is the empty string between a character at an odd positions and a character at an even position. That's not exactly straightforward to match, but it's possible.

$ perl -E'say for split /(?!^|\z)(?(?{ pos()%2 })(?!))/, "0102030405"' 01 02 03 04 05 06

Here, it's simpler just to match what you want grab rather than what separates them, so just use a m//g:

$ perl -E'say for "0102030405" =~ /(..?)/sg' 01 02 03 04 05 06

Replies are listed 'Best First'.
Re^2: Split on every second character
by gri6507 (Deacon) on Feb 12, 2010 at 23:13 UTC
    No wonder getting the split() operator to work correctly was difficult. Thank you!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (6)
As of 2024-04-20 00:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found