On 11/9/09 Mon Nov 9, 2009 5:03 AM, "rithu" <[email protected]>
scribbled:
> Hi,
>
> Please help me to split a string as follows..
>
> my $line = "abcdefghijkl"
>
> the expected output should be like:
>
> ab
> ef
> ij
>
>
> The logic is like alternate 2 characters should be removed....
split would not be the best tool for this task. I would use substr in an
indexed for loop:
my @output;
for( my $i = 0; $i < length($line); $i += 4 ) {
push( @output, substr($line,$i,2) );
}
However, here is a shortened form using regular expression:
my @output = $line =~ m{ \G (..) .. }gx;
Verify how either of these works when you do not have a multiple of 2
characters in your input.
--
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
http://learn.perl.org/