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

Hello monks. I'm using split to handle tabulated files. Rows have simple syntax
Name	number	number	number
Name	number	number	number
...
Now the number fields can be empty so basicly i have some rows which have only name and tabs. I've noticed that split does not return empty cells properly. There is a bit about using pattern " ", it will skip leading whitespace. but that should not apply here. I've added a simple script which shows the problem.
#!/usr/bin/perl # # testing split # use warnings; use strict; my $a="val "; print "'$a'\n"; my @a=split (/\t/,$a); my $val=scalar (@a); print "$val '@a'\n";


UPDATE:
I've created 3 as a result of browser fumbling on my part, can there extra 2 be remove please. And how can i do this myself?

Replies are listed 'Best First'.
Preserving empty fields in split
by pjf (Curate) on Feb 05, 2004 at 12:11 UTC
    G'day Hena,

    If you don't want split to remove trailing empty fields, then you can supply it with a third argument, set to a negative value. Normally this is the maximum number of fields you wish split to generate, but Perl will interpret a negative number to mean leaving all fields intact, including empty ones.

    In your particular example, you would change your split line to:

    my @a = split(/\t/,$a,-1);

    For more information on this feature, check out perldoc -f split or the online documentation on this function.

    All the very best,

Re: Working of split
by Abigail-II (Bishop) on Feb 05, 2004 at 12:04 UTC
    Your example isn't clear. What is following 'val' in the string? Spaces? Tabs? Spaces and tabs? Furthermore, what does it print? (Don't assume everyone is going to cut and paste your code and run it - do the people potentially answering your question a favour, and include the output). And finally, what do you expect it to print?

    Perhaps you got bitten by the fact that split with 2 parameters deletes empty trailing fields. But I can't determine that from your post.

    Abigail

      Ups... right. Well there is a name and 4 tabs. And return value is just one cell (name), no empty ones are returned. How can i change it so that it returns empty ones as well?
        So, I guessed right. I also mentioned something about the number of arguments of split. Why don't you consult its manual page, and come back only if the manual page isn't clear?

        Abigail

        well, for me, it returned the correct number:

        1 'name

        '

        3 'name

        '

        3 'name val val

        '

        This is what I had the data file: name

        name

        name val val

        firt one is just the name

        second one is name and two tabs

        third line also has two tabs

        Here is your code with while loop

        #!/usr/bin/perl # # testing split # use warnings; use strict; while (<>) { my @a=split (/\t/,$_); my $val=scalar (@a); print "$val '@a'\n"; }
Re: Working of split
by Zaxo (Archbishop) on Feb 05, 2004 at 12:12 UTC

    Perl split will suppress trailing empties unless you give it the third argument. Does this do what you want?

    $_ = "val\t\t\t"; my @a = split /\t/, $_, 4; print scalar( @a), $/;

    After Compline,
    Zaxo