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?
Split with a negative limit will preserve trailing empty fields.
@fields = split(/,/, "a,,", -1);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With