Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > split-like thing

Reply
Thread Tools

split-like thing

 
 
john
Guest
Posts: n/a
 
      04-21-2009
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!

 
Reply With Quote
 
 
 
 
Frank Seitz
Guest
Posts: n/a
 
      04-21-2009
john wrote:
>
> 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
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
 
Reply With Quote
 
 
 
 
Frank Seitz
Guest
Posts: n/a
 
      04-21-2009
john wrote:
>
> 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
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-21-2009
On Tue, 21 Apr 2009 23:51:47 +0200, Frank Seitz <(E-Mail Removed)> wrote:

>john wrote:
>>
>> 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;

^ ^
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

 
Reply With Quote
 
Frank Seitz
Guest
Posts: n/a
 
      04-21-2009
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> On Tue, 21 Apr 2009 23:51:47 +0200, Frank Seitz <(E-Mail Removed)> wrote:
>> john wrote:
>>> 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;

> ^ ^
> 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
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
 
Reply With Quote
 
Lars Eighner
Guest
Posts: n/a
 
      04-21-2009
In our last episode, <(E-Mail Removed)>, the lovely and
talented john broadcast on comp.lang.perl.misc:

> 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")


sed


--
Lars Eighner <http://larseighner.com/> (E-Mail Removed)
91 days since Rick Warren prayed over Bush's third term.
Obama: No hope, no change, more of the same. Yes, he can, but no, he won't.
 
Reply With Quote
 
News123
Guest
Posts: n/a
 
      04-22-2009
Lars Eighner wrote:
> In our last episode, <(E-Mail Removed)>, the lovely and
> talented john broadcast on comp.lang.perl.misc:
>
>> 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")

>
> sed
>
>


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?
 
Reply With Quote
 
smallpond
Guest
Posts: n/a
 
      04-22-2009
On Apr 21, 6:14 pm, Lars Eighner <(E-Mail Removed)> wrote:
> In our last episode, <(E-Mail Removed)>, the lovely and
> talented john broadcast on comp.lang.perl.misc:
>
> > 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")

>
> sed
>


sed does not have an array data type. awk would be better,
although regexp are not as powerful as perl's.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
one thing solved, but other terrible thing occur... Zam ASP General 1 03-14-2005 06:09 PM
The thing I hate about FF and TB Tony Raven Firefox 7 12-13-2004 02:32 AM
Odd thing with nightly builds of Firefox? Chaos Master Firefox 5 08-07-2004 05:43 PM
Spastic Jittery thing in preview pane Paul Furman Firefox 4 10-04-2003 12:56 AM
Converted to Mozilla but one thing missing - key macro/substitution no-spam Firefox 5 07-29-2003 08:07 PM



Advertisments