How can I make split(/\t/, $STRING) detect empty items when $STRING ends with "\t\t"?
Using Perl script, I am trying to split strings based on as a separator between items then count and use these items. But, when the strings end with a combination s, the script doesn't count empty items.
Example:
string="value1\tvalue2\t\t\t\t" (value1, value2 and 4 empty items)
but it counts 2 items (value1, value2):
$STRING="value1\tvalue2\t\t\t\t";
print $STRING."\n";
my @F = split(/\t/, $STRING);
print scalar(@F)."\n";
# The number of items must match the number of header name
if( scalar(@F) == 6 )
{
print "Done \n";
}
the code prints:
value1 value2
2
while what is expected is:
value1 value2
6
Change the split line by adding -1 as a parameter:
my @F = split(/\t/, $STRING, -1);
Output:
value1 value2
6
Done
Per perldoc split, the parameters to split are PATTERN, EXPR, LIMIT. In your current situation, "LIMIT is omitted so "trailing empty fields are stripped."
By adding the -1 value for LIMIT:
If LIMIT is negative, it is treated as if it were instead arbitrarily large; as many fields as possible are produced.
So a negative final argument prevents split from discarding trailing fields.
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