Split by length

P

Papago

Is there a single Perl command that will split a string into an array by
number of characters? For example, if I had the string:

$string = "monkey";

I could specify a lenght, such as 2, and it would put every pair of 2
characters into an array that looks something like this:

$array[0] = "mo";
$array[1] = "nk";
$array[3] = "ey";

Is that possible?
 
D

David K. Wall

Papago said:
Is there a single Perl command that will split a string into an array by
number of characters? For example, if I had the string:

$string = "monkey";

I could specify a lenght, such as 2, and it would put every pair of 2
characters into an array that looks something like this:

$array[0] = "mo";
$array[1] = "nk";
$array[3] = "ey";

Is that possible?

Yes. You can do it the easy way by capturing stuff with a regex or you can do
it a longer way using substr(). I'm sure there are many other ways as well.
What way did your teacher want you to use?
 
F

Fayland

I wrote some code as folows:
-----------------------
$a = "avcdfadad";

foreach ($a =~ /.{1,2}/g) { # if u donn't want to push the last one char to
array,use /.{2}/
push (@a, $_);
}

foreach (@a) {
print "$_\n";
}

print scalar @a;
 
G

Gunnar Hjalmarsson

Fayland said:
foreach ($a =~ /.{1,2}/g) { # if u donn't want to push the last one
char to array,use /.{2}/
push (@a, $_);
}

The foreach loop is redundant:

@a = $a =~ /.{1,2}/g;
 
T

Tad McClellan

$a = "avcdfadad";

It's weird


No it isn't.

If you had told us what you thought was weird, we could have perhaps
explained it.

At it is, it is doing exactly what is expected, so we cannot help
repair whatever misunderstanding you have...

that I use "@a = split(/(.{1,2})/,$a);", it give me 10 elements
of array.


You get 5 (empty) fields, and 5 separators, just what the function's
docs say you should.

That adds up to 10 (and I didn't even have to take off my shoes
to calculate that :)
 
F

Fayland

I read the perlfunc "split" again,and understand why the empty fields
produced.
Thanks.And I want to ask weather there is a way to skip the empty
fields(undef),how to write the regexp?
not /(.{1,2})/ but what?
 
A

Anno Siegel

Papago said:
Is there a single Perl command that will split a string into an array by
number of characters? For example, if I had the string:

$string = "monkey";

I could specify a lenght, such as 2, and it would put every pair of 2
characters into an array that looks something like this:

$array[0] = "mo";
$array[1] = "nk";
$array[3] = "ey";

my @l = unpack '(a2)*', $string;

Anno
 
T

Tad McClellan

I read the perlfunc "split" again,and understand why the empty fields
produced.
Thanks.And I want to ask weather there is a way to skip the empty
fields(undef),


@a = grep length, split(/(.{1,2})/,$a);


but a m// in list context is Much Better for this task.
 
A

Anno Siegel

bowsayge said:
Papago said to us:
Is there a single Perl command that will split a string into an array by
number of characters? For example, if I had the string:

$string = "monkey";

I could specify a lenght, such as 2, and it would put every pair of 2
characters into an array that looks something like this:

$array[0] = "mo";
$array[1] = "nk";
$array[3] = "ey";

Is that possible?

Try this:

my $string = 'monkey';
my $length = 2;
my @array = $string =~ m/.{$length}/g;
print "@array\n";

That will ignore a possible remainder of fewer than $length characters
if the string length isn't divisible by $length. This may be the desired
behavior, the OP didn't say. In case it isn't, using m/.{1,$length}/g
will always catch the last piece, even if it's shorter than $length.

Anno
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top