Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

•Re: find real length of an array

by merlyn (Sage)
on Mar 16, 2003 at 15:59 UTC ( [id://243480]=note: print w/replies, xml ) Need Help??


in reply to find real length of an array

The commas on the right of your intiialization do not create null elements. They are tossed as a syntax convenience. So both of your example arrays do indeed have only 9 elements in them. You can see that by pushing something onto either one: the length will go to 10.

-- Randal L. Schwartz, Perl hacker
Be sure to read my standard disclaimer if this is a reply.

Replies are listed 'Best First'.
Re: •Re: find real length of an array
by hackmare (Pilgrim) on Mar 16, 2003 at 16:51 UTC

    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

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://243480]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-20 00:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found