Work for split function when last character is terminator

I have this data row with 20 fields:

my $data = '54243|601|0|||0|N|0|0|0|0|0||||||99582|';

I use this to split data:

my @data = split ('\|'), $data;

However, instead of 20 pieces of data, you get only 19:

print scalar @data;

I could manually pushempty the string on @dataif the last character is this |, but I wonder if there is an easier way.

+4
source share
1 answer

Do

my @data = split /\|/, $data, -1;

The -1 option tells split to include empty trailing fields.

( , $data . , split , regex , .)

+8

Source: https://habr.com/ru/post/1668981/


All Articles