On Oct 11, 6:45 pm, elroyerni <davechu...@gmail.com> wrote:
> I'm trying to assign to a variable the number of elements in a
> array for example. I have array with data:
>
> my @array_data;
>
> I want to assign the number of elements in the array to a
> variable
my $size = @array_data;
> i tried this and looked it up and can't find a solution
perldoc perldata:
If you evaluate an array in scalar context, it returns the
length of the array. (Note that this is not true of lists,
which return the last value, like the C comma operator, nor
of built-in functions, which return whatever they feel like
returning.)
> Say array_data contains {0,1,2,3,4,5} i want to assign the number
> of elements to a variable, in this case "6" to a variable.
>
> i tried this and failed...
>
> my $total = `0 + @{$results}`;
There are a number of things wrong with that expression. For one,
what the heck is $results? You've only mentioned the array
@array_data. You're using $results there as though it's an array
reference. Is it? How is it being created.
For two, you're taking the result of that 0 + @{$results} expression,
and executing it as an external program. Then you're taking the
output of that program and assigning it to $total. That's what the
`...` quotes do.
Paul Lalli