Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

length of array not understood

Status
Not open for further replies.

bobbybobbertson

Programmer
Nov 9, 2001
119
US
Can someone tell me why the output to this program is "2"?
I'm expecting to get "12".


my @messages = qw(ones twos three four five six seven eight nine ten eleven twelves);

print length @messages;
 
You can't use the length function on arrays - if you want the number of elements in an array, do this:

print scalar(@array);

That forces the array to be interpreted in scalar context rather than list context.

Also, heres a cool trick:

print ~~@array;

A side-effect of the ~ bit-wise negation operator is that it forces it's operand into scalar integer context. Since we are doing it twice, we get the orginal value in scalar context. Trick I learned from a fellow named marcus who learned it at a Damian Conway conference.

--jim
 
a more boring, but sorta standard, way of doing this is
$#array


Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Keep in mind though that using that method will return the number of the last subscript in the array. SO if @this_array has 9 elements in it (0..8) then $#this_array will have the value 8, one less than the number of elements in the array. Use like this:

$element_count = $#array_name + 1;

Also, you can pre-extend an array by assigning to the value:

$#array_name = 100;

This will tell Perl to allocate a block of memory for an array you know will be large. Possibly facilitating more efficient operation.

--jim
 
Oops.... quite true, $# is the index of the last element rather than the length; I always get that wrong. Mike
______________________________________________________________________
"Experience is the comb that Nature gives us after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
The reason you're getting "2" is because when you use an array in a scalar context (the argument to length must be a scalar, so perl is putting the array into a scalar context) you get the length of the array, which is "12". Then the length function gives you the length of the scalar context of the array, meaning it's taking the length of the string "12", which is 2.

There will be a short quiz tomorrow morning...
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
and since you can force an array into a scalar context its conceivable to do this:

print my $num = @array;

heh
 
You can also do this:
Code:
print scalar(@array);
Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
[thumbsup2] It's OK! Just didn't want you to think I missed one of the more appropriate and straight forward solutions to the question posted.

--jim
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top