How do I get the total items in an array, not the last id?
None of two ways I found to do this works:
my @a; # Add some elements (no consecutive ids) $a[0]= '1'; $a[5]= '2'; $a[23]= '3'; print $#a, "\n"; # Prints 23 print scalar(@a), "\n"; # Prints 24
I expected to get 3...
line 11 (#1) (W syntax) You used length() on either an array or a hash when you probably wanted a count of the items. Array size can be obtained by doing: scalar(@array); The number of items in a hash can be obtained by doing: scalar(keys %hash); Please always use warnings and use strict!
Just divide the number of allocated bytes by the number of bytes of the array's data type using sizeof() . int numArrElements = sizeof(myArray) / sizeof(int);
$#array is the subscript of the last element of the array (which is one less than the length of the array, since arrays start from zero). Assigning to $#array changes the length of the array @array, hence you can destroy (or clear) all values of the array between the last element and the newly assigned position.
$length = @foods; The variable $length will now hold the length of the Perl array. This is referred to as "implicit scalar conversion", and it is probably the easiest and most common way to determine the Perl array length.
Edit: Hash versus Array
As cincodenada correctly pointed out in the comment, ysth gave a better answer: I should have answered your question with another question: "Do you really want to use a Perl array? A hash may be more appropriate."
An array allocates memory for all possible indices up to the largest used so-far. In your example, you allocate 24 cells (but use only 3). By contrast, a hash only allocates space for those fields that are actually used.
Array solution: scalar grep
Here are two possible solutions (see below for explanation):
print scalar(grep {defined $_} @a), "\n"; # prints 3 print scalar(grep $_, @a), "\n"; # prints 3
Explanation: After adding $a[23]
, your array really contains 24 elements --- but most of them are undefined (which also evaluates as false). You can count the number of defined elements (as done in the first solution) or the number of true elements (second solution).
What is the difference? If you set $a[10]=0
, then the first solution will count it, but the second solution won't (because 0 is false but defined). If you set $a[3]=undef
, none of the solutions will count it.
Hash solution (by yst)
As suggested by another solution, you can work with a hash and avoid all the problems:
$a{0} = 1; $a{5} = 2; $a{23} = 3; print scalar(keys %a), "\n"; # prints 3
This solution counts zeros and undef values.
It sounds like you want a sparse array. A normal array would have 24 items in it, but a sparse array would have 3. In Perl we emulate sparse arrays with hashes:
#!/usr/bin/perl use strict; use warnings; my %sparse; @sparse{0, 5, 23} = (1 .. 3); print "there are ", scalar keys %sparse, " items in the sparse array\n", map { "\t$sparse{$_}\n" } sort { $a <=> $b } keys %sparse;
The keys
function in scalar context will return the number of items in the sparse array. The only downside to using a hash to emulate a sparse array is that you must sort the keys before iterating over them if their order is important.
You must also remember to use the delete
function to remove items from the sparse array (just setting their value to undef is not enough).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With