Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Simple Array question

Reply
Thread Tools

Simple Array question

 
 
elroyerni
Guest
Posts: n/a
 
      10-11-2007
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!

 
Reply With Quote
 
 
 
 
Lars Eighner
Guest
Posts: n/a
 
      10-11-2007
In our last episode,
<(E-Mail Removed) .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 <http://larseighner.com/> <http://myspace.com/larseighner>
Countdown: 466 days to go.
What do you do when you're debranded?
 
Reply With Quote
 
 
 
 
usenet@DavidFilmer.com
Guest
Posts: n/a
 
      10-11-2007
On Oct 11, 3:51 pm, Lars Eighner <(E-Mail Removed)> 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)



 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-12-2007
On Oct 11, 6:51 pm, Lars Eighner <(E-Mail Removed)> wrote:
> In our last episode,
> <(E-Mail Removed) .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

 
Reply With Quote
 
Paul Lalli
Guest
Posts: n/a
 
      10-12-2007
On Oct 11, 6:45 pm, elroyerni <(E-Mail Removed)> 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

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      10-12-2007
elroyerni <(E-Mail Removed)> 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 <(E-Mail Removed)>
(remove .invalid and reverse each component for email address)
clpmisc guidelines: <URL:http://www.augustmail.com/~tadmc/clpmisc.shtml>

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      10-12-2007
On Thu, 11 Oct 2007 22:45:26 -0000, elroyerni <(E-Mail Removed)>
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^<R<Y]*YB='
..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
const and array of array (of array ...) Mara Guida C Programming 3 09-03-2009 07:54 AM
simple, simple array question Peter Bailey Ruby 7 04-08-2008 01:54 PM
length of an array in a struct in an array of structs in a struct in an array of structs Tuan Bui Perl Misc 14 07-29-2005 02:39 PM
Length of Array of Array of Array Tom Perl Misc 3 12-20-2004 05:23 PM
Re: Simple Simple question!!! ashelley@inlandkwpp.com ASP .Net 0 06-25-2004 04:18 PM



Advertisments