IJKin has asked for the wisdom of the Perl Monks concerning the following question:

use strict; my $tst_str = "1~2~3~4~5~~~~~~~~~~"; my @ray = split(/~/,$tst_str); my $len = scalar(@ray); print "len - $len\n";
I get 5 when I run this: Solaris, Perl 5.8.8 How can I get the TRUE number of fields?

Replies are listed 'Best First'.
Re: Split returns UPTO last non-blank field
by Perlbotics (Archbishop) on Sep 28, 2011 at 20:55 UTC

    Try changing my @ray = split(/~/,$tst_str); to my @ray = split(/~/,$tst_str,-1);

Re: Split returns UPTO last non-blank field
by Anonymous Monk on Sep 28, 2011 at 20:58 UTC
    Common pitfall for split, so read split documentation :)
    use strict; my $tst_str = "1~2~3~4~5~~~~~~~~~~"; my @ray = split(/~/,$tst_str,-1); my $len = scalar(@ray); print "len - $len\n"; __END__ len - 15