[Israel.pm] Inconsistent 'split' behavior

Meir Guttman meir at guttman.co.il
Mon Mar 4 06:05:48 PST 2013


Thank you Gaal!

Once again, I constantly forget the RTFM directive. ‘split’ (and ‘join’) are so intuitive that I never bothered to lookup the doc.

Thank all of you

Meir

 

From: perl-bounces at perl.org.il [mailto:perl-bounces at perl.org.il] On Behalf Of Gaal Yahas
Sent: יום ב 04 מרץ 2013 15:35
To: Perl in Israel
Subject: Re: [Israel.pm] Inconsistent 'split' behavior

 

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/a403e14c/attachment.htm 


More information about the Perl mailing list