myforwik
myforwik

Reputation: 23

perl split behaviour differences?

I installed active perl same version on windows XP and windows 7.

I have found that on windows XP and ubuntu split will return empty array elements, but on windows 7 it won't. For example:

my @array;
my $item = "test,,,,";
@array = split(/,/,$item);
print $#array;

will print 4 and the array will have 3 empty elements, but on windows 7 it always prints 0 and has only 1 element. Does anyone know why this happens and what I should be using to make the perl script more portable?

Upvotes: 2

Views: 283

Answers (1)

musiKk
musiKk

Reputation: 15189

According to the documentation of split the Windows 7 version is correct:

By default, empty leading fields are preserved, and empty trailing ones are deleted. (If all fields are empty, they are considered to be trailing.)

So if you split e.g. ",,,test,,,," then your example should print 3. (Tested with 5.10.1 on Ubuntu 10.04.)

Upvotes: 2

Related Questions