Skip navigation.
Home
Your source for Perl tips, howto's, faq and tutorials
( categories: )

By default, split keeps empty leading fields. To remove them, use the following:

@list = grep length, split /PATTERN/, $string;

Example:

#!/usr/bin/perl
 
$string = ",,,fourth,fifth";
 
#-- list with 5 elements
@list_with_leading_empty = split /,/, $string;
print "Number of fields: " . scalar @list_with_leading_empty . "\n";
 
#-- list with 2 elements
@list_without_leading_empty = grep length, split /,/, $string;
print "Number of fields: " . scalar @list_without_leading_empty . "\n";