Simple Array question

E

elroyerni

Hi -

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, i
tried this and looked it up and can't find a solution :(

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}`;

Any help?

Thanks!
 
L

Lars Eighner

In our last episode,
the lovely and talented elroyerni
broadcast on comp.lang.perl.misc:
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, i
tried this and looked it up and can't find a solution :(
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}`;
Any help?

Wow, it's a little late to be doing your first week's homework.

Why doesn't $#array_data + 1 work for you?
 
U

usenet

Why doesn't $#array_data + 1 work for you?

Many programmers would just use @array_data in a scalar context, or
force it with scalar(@array_data). When an array is evaluated in
scalar context it returns the number of elements it contains.
 
P

Paul Lalli

In our last episode,
the lovely and talented elroyerni
broadcast on comp.lang.perl.misc:


Wow, it's a little late to be doing your first week's homework.

Why doesn't $#array_data + 1 work for you?

A little late for you to have to be told you shouldn't use that.
Somewhere, someway, some schmuck will edit your code and change the $
[ variable, and all of a sudden, $#foo will no longer be "one less
than the size". Because $#foo is not intended to be "one less than
the size". It actually is "the last index of".

Use the array in scalar context. That is defined to be the size.

Paul Lalli
 
P

Paul Lalli

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
 
A

A. Sinan Unur

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, i
tried this and looked it up and can't find a solution :(

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.

my @array_data = (0 .. 5);
my $total = @array_data;

print "Total = $total\n";
i tried this and failed...


my $total = `0 + @{$results}`;

Read perldoc -f system to find out what backticks actually do (or
consult perldoc perlop for the qx operator).

Sinan
 
M

Michele Dondi

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}`;

And were is array_data now? What is it anyway? Do you mean @array_data
perhaps? However what you wrote would work... if

(i) you were not having a shell execute it;
(ii) $results is a arrayref;

and last,

(iii) the C<0 +> bit is not necessary at all.


Michele
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,571
Members
45,045
Latest member
DRCM

Latest Threads

Top