I have this line of data with 20 fields:
my $data = '54243|601|0|||0|N|0|0|0|0|0||||||99582|';
I'm using this to split the data:
my @data = split ('\|'), $data;
However, instead of 20 pieces of data, you only get 19:
print scalar @data;
I could manually push
an empty string onto @data
if the last character is a |
but I'm wondering if there is a more perlish way.
Do
my @data = split /\|/, $data, -1;
The -1 tells split to include empty trailing fields.
(Your parentheses around the regex are incorrect, and lead to $data not being considered a parameter of split. Also, with one exception, the first argument of split is always a regex, so it is better to specify it as a regex not a string that will be interpreted as a regex.)
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