split-like thing

J

john

Hi,

If I have a string "abcdefgh", what's the best way to get an array with
the characters in pairs? ("ab", "cd", "ef", "gh")

Thanks!
 
F

Frank Seitz

john said:
If I have a string "abcdefgh", what's the best way to get an array with
the characters in pairs? ("ab", "cd", "ef", "gh")

(@arr) = $str =~ /(..)/g;

Frank
 
F

Frank Seitz

john said:
If I have a string "abcdefgh", what's the best way to get an array with
the characters in pairs? ("ab", "cd", "ef", "gh")

@arr = $str =~ /(..)/g;

Frank
 
S

sln

@arr = $str =~ /(..)/g;
^ ^
Don't need to capture the whole pattern, that is the default in list context,
otherwise subpatterns as such will get it: @arr = $str =~ /.(.)/g;

This also works as well:
@arr = $str =~ /.{2}/g;

-sln
 
F

Frank Seitz

^ ^
Don't need to capture the whole pattern, that is the default in list context,
otherwise subpatterns as such will get it: @arr = $str =~ /.(.)/g;

Thank you for the improvement.
This also works as well:
@arr = $str =~ /.{2}/g;

TMTOWTDI

Frank
 
N

News123

Lars said:

Neither
echo "abcdefgh" | sed
nor
echo "abcdefgh" | perl -e 'sed'

returns the expected result. ;-)

Just for fun" Could you show me how to do it with sed?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top