http://qs1969.pair.com?node_id=243493


in reply to •Re: find real length of an array
in thread find real length of an array

Thanks for the quick reply.

In my real source code in the offending application, the offending array is constructed from a split of a tab-delimited line. I am using split("\t",$line,99999) to generate the line.

Hmmm... When I run the test below on the command line, it seems to behave correctly I have a feeling that I need to take a close look at the offending code. All the splits which I am using to generate the array are providing the field number option, which inserts undefs into the placeholders. Well, at least the answers I got here helped me cross out some of the possible sources of error. Thanks for the help.

bash-2.03$ cat test.pl #!/usr/bin/perl my $line = "hello\tworld\tI\tam\ta\tline\twithout\ttabs"; my $line_2 = "hello\tworld\tI\tam\ta\tline\twith\ttabs\t\t\t\t"; my @array = split("\t",$line,9999); my @array_2 = split("\t",$line_2,9999); my @array_3 = split("\t",$line_2); print "Scalar of array 1 =",scalar @array,"\n"; print "Scalar of array 2 =",scalar @array_2,"\n"; print "Scalar of array 3 =",scalar @array_3,"\n"; bash-2.03$ perl test.pl Scalar of array 1 =8 Scalar of array 2 =12 Scalar of array 3 =8 bash-2.03$

Ronan
www.roasp.com

Replies are listed 'Best First'.
Re^3: find real length of an array
by Aristotle (Chancellor) on Mar 16, 2003 at 19:04 UTC
    This has nothing to do with array behaviour in general, but solely with the way split behaves. Note that if you really want to conserve all trailing fields, instead of passing 99999, you should give it -1. This is documented in perlfunc:
    If LIMIT is specified and positive, splits into no more than that many fields (though it may split into fewer). If LIMIT is unspecified or zero, trailing null fields are stripped (which potential users of pop would do well to remember). If LIMIT is negative, it is treated as if an arbitrarily large LIMIT had been specified.

    Makeshifts last the longest.

      Aristotle,

      Thanks for the '-1' pointer. I wasn't aware of it.

      I still have to check my coriginal code because with it I am splitting with a large limit value but am still having the issue of misrepresented array size for arrays generated by split. I will look into my code this AM and report back to the monks to clarify what I find to be the source (programmer -me- error or odd behaviour).

      Clearly probabilities lean towards the former.

      hackmare.