Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent split from removing empty elements

Tags:

perl

I see that perl removes all the empty elements when i split a string. Is this the expected behavior? I feel this should be left to the programmer to deal with empty fields

> perl -e '@fields = split(/,/, "a,,"); print "$#fields\n";'
0
> perl -e '@fields = split(/,/, "a,b,c"); print "$#fields\n";'
2

Is there a way to prevent this?

like image 231
SAN Avatar asked Aug 06 '26 05:08

SAN


1 Answers

Split with a negative limit will preserve trailing empty fields.

@fields = split(/,/, "a,,", -1);
like image 63
ysth Avatar answered Aug 08 '26 21:08

ysth



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!