Simple Array question

Simple Array question

am 12.10.2007 00:45:26 von 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!

Re: Simple Array question

am 12.10.2007 00:51:48 von Lars Eighner

In our last episode,
<1192142726.763354.235610@e34g2000pro.googlegroups.com>,
the lovely and talented elroyerni
broadcast on comp.lang.perl.misc:

> 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?

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

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

--
Lars Eighner
Countdown: 466 days to go.
What do you do when you're debranded?

Re: Simple Array question

am 12.10.2007 01:40:56 von usenet

On Oct 11, 3:51 pm, Lars Eighner wrote:
> 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.

--
The best way to get a good answer is to ask a good question.
David Filmer (http://DavidFilmer.com)

Re: Simple Array question

am 12.10.2007 02:24:52 von Paul Lalli

On Oct 11, 6:51 pm, Lars Eighner wrote:
> In our last episode,
> <1192142726.763354.235...@e34g2000pro.googlegroups.com>,
> the lovely and talented elroyerni
> broadcast on comp.lang.perl.misc:
>
> > 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?
>
> 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

Re: Simple Array question

am 12.10.2007 02:29:10 von Paul Lalli

On Oct 11, 6:45 pm, elroyerni 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

Re: Simple Array question

am 12.10.2007 02:31:25 von 1usa

elroyerni wrote in news:1192142726.763354.235610
@e34g2000pro.googlegroups.com:

> 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

--
A. Sinan Unur <1usa@llenroc.ude.invalid>
(remove .invalid and reverse each component for email address)
clpmisc guidelines:

Re: Simple Array question

am 12.10.2007 02:45:30 von sln

On Thu, 11 Oct 2007 22:45:26 -0000, elroyerni wrote:

>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!

use strict;
use warnings;

my @array = (0,1,2,3,4);
my $tt = @array;

print "@array $tt\n";
print @array.$tt;

---output:
0 1 2 3 4 5
55

Re: Simple Array question

am 12.10.2007 03:07:10 von sln

On Thu, 11 Oct 2007 17:45:30 -0700, sln@netherlands.co wrote:

>On Thu, 11 Oct 2007 22:45:26 -0000, elroyerni wrote:
>
>>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!
>
>use strict;
>use warnings;
>
>my @array = (0,1,2,3,4);
>my $tt = @array;
>
>print "@array $tt\n";
>print @array.$tt;
>
>---output:
>0 1 2 3 4 5
>55

and,

print @array*$tt;

25

Re: Simple Array question

am 12.10.2007 15:04:00 von Michele Dondi

On Thu, 11 Oct 2007 22:45:26 -0000, elroyerni
wrote:

>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
--
{$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
(($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^ ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER 256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,