in reply to Working of split

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

Replies are listed 'Best First'.
Re: Re: Working of split
by Hena (Friar) on Feb 05, 2004 at 12:07 UTC
    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

        Yes, is spotted it now... *reading*
        So -1 (any negative number) in limit in split will return all the empty trailing columns. Sometimes i feel like an braindead :P. Huoh.
      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"; }