[Israel.pm] Inconsistent 'split' behavior

Gaal Yahas gaal at forum2.org
Mon Mar 4 05:35:22 PST 2013


Try split /\t/, $str, -1. See perldoc -f split for more details.

On Mon, Mar 4, 2013 at 2:32 PM, Meir Guttman <meir at guttman.co.il> wrote:

> 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
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 
Gaal Yahas <gaal at forum2.org>
http://gaal.livejournal.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20130304/034c4412/attachment.htm 


More information about the Perl mailing list