Mahurshi Akilla wrote:
Is there an easy way (without writing our own proc) to split a string
based on number of occurances of a character ?

for example:

$my_string = "a;b;c;d;e;f;g;h"

@regsplitarray = split (/;/, $my_string)   splits based on every 1
occurance of ";"

what if i want it to do 2 at a time ?  e.g. $regsplitarray[0] =
"a;b" .. $regsplitarray[1] = "c;d" .. and so on..

In this example, I used 2..  It could be n in general.  Is there a way
to do this without writing our own procedure ?

I think I would split them into single items and then pair them up again.
The program below shows the technique.

HTH,

Rob


use strict;
use warnings;

my $my_string = "a;b;c;d;e;f;g;h";

my @regsplitarray;
{
 my @data = split /;/, $my_string;
 while (@data) {
   push @regsplitarray, join ';', splice @data, 0, 2;
 }
}

print "$_\n" foreach @regsplitarray;

**OUTPUT**

a;b
c;d
e;f
g;h

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to