LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-07-2005, 03:10 PM   #1
AM1SHFURN1TURE
Member
 
Registered: Sep 2004
Location: US
Distribution: Debian
Posts: 56

Rep: Reputation: 15
perl - get number of elements in an array


is there any way to get the number of elements an array has?
say i have @array = ("one","two","three","four","five","six")
is there a possible way to get the number of how many elements there are from
$array[0..$#array] and then assign it to a variable?
 
Old 03-07-2005, 03:23 PM   #2
david_ross
Moderator
 
Registered: Mar 2003
Location: Scotland
Distribution: Slackware, RedHat, Debian
Posts: 12,047

Rep: Reputation: 79
Just use:
$numelements=@array;
 
Old 03-07-2005, 03:38 PM   #3
AM1SHFURN1TURE
Member
 
Registered: Sep 2004
Location: US
Distribution: Debian
Posts: 56

Original Poster
Rep: Reputation: 15
oh goodness, had no idea all I had to do was assign some scalar to it
thanks for the reply!
 
Old 03-07-2005, 03:59 PM   #4
acid_kewpie
Moderator
 
Registered: Jun 2001
Location: UK
Distribution: Gentoo, RHEL, Fedora, Centos
Posts: 43,417

Rep: Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985Reputation: 1985
in longhand you would explicitly request a scalar interpretation of it, e.g "$num = scalar @array". it's not needed, but does make it slightly clearer for when you can't be bothered to comment your own code.
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Deleting elements from array in perl with splice signalno9 Programming 2 08-16-2005 10:57 PM
odd behaviour of array elements in c++ markhod Programming 4 03-14-2005 09:58 AM
arrays of elements with [gcc4]array type has incomplete element type lmmix Linux - Software 0 02-26-2005 08:07 AM
repeated number in a 4x4 array xbaez Programming 3 11-21-2004 11:03 PM
PERL: Size of an array of an Array inspleak Programming 2 03-10-2004 02:24 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:46 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration