[Israel.pm] Inconsistent 'split' behavior

Meir Guttman meir at guttman.co.il
Mon Mar 4 04:32:47 PST 2013


Hello Perlers.
I recently discovered what seems to me an inconsistent behavior when
splitting Tab-separated-Values (TSV). If the last column has an empty
string, no empty string is added to the 'split' array. However, when a
center column has an empty string (simply two consecutive tab-characters) it
is conserved.
Is that behavior as it should be?
Here is a small script to show it:

use strict;
use warnings;
use Data::Dumper::AutoEncode; # This one supports Unicode

my $tsv_line = "col_1\tcol_2\tcol_3";
print "Regular TSV string: '$tsv_line'\n";
my @split_line = split("\t", $tsv_line);
print eDumper(\@split_line), "\n";

my $missing_col_tsv_line = "col_1\tcol_2\t";
print "missing LAST column TSV string: '$missing_col_tsv_line'\n";
my @split_missing_col_line = split("\t", $missing_col_tsv_line);
print eDumper(\@split_missing_col_line), "\n";

my $missing_middle_col = "col_1\t\tcol_3";
print "missing MIDDLE column TSV string: '$missing_middle_col'\n";
my @split_missing_middle_col = split("\t", $missing_middle_col);
print eDumper(\@split_missing_middle_col), "\n";

Meir



More information about the Perl mailing list