Would Larry do something that stupid? ;-)
The code you have will work fine, without creating a duplicate array. You can also get the highest index in the array with $#$aref, which, if you haven't changed the default least index from 0 (by setting $[) will be one less than the number of elements in the array.
| [reply] [d/l] [select] |
| [reply] [d/l] [select] |
I think that $#$aref does the trick - you might need to add one to give the number of elements. | [reply] |
I suspect (scalar @{$aref}) actually builds the array again, just to size it. The array is large, so this is slow.....
Well, no. scalar @$array_reference will do what you want it to do. Dereferencing is not copying, you're just re-shaping it temporarily, so it can be used as if it were a real array (surprise: with @$ref, you HAVE a real array in every way. It's not a copy, it's the array $ref is referencing to).
The reference doesn't hold the actual array, it merely refers (points) to it. The array itself is somewhere else in memory, and may or may not have a normal @foo name, or maybe multiple. By dereferencing, you tell Perl to use the array instead of the reference.
U28geW91IGNhbiBhbGwgcm90MTMgY
W5kIHBhY2soKS4gQnV0IGRvIHlvdS
ByZWNvZ25pc2UgQmFzZTY0IHdoZW4
geW91IHNlZSBpdD8gIC0tIEp1ZXJk
| [reply] |
| [reply] |
$size = $#$aref + 1;
Quick, simply, concise, just the way we monks like it.
Tradez
"Never underestimate the predicability of stupidity"
- Bullet Tooth Tony, Snatch (2001) | [reply] [d/l] |
$size = @array; #Gives size of array.
Can be used as follows ...
for($i=0;$i<$size;$i++)
{
#Do work here;
}
| [reply] [d/l] [select] |