- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Array Size in Perl
The size of an array in Perl can be determined using the scalar context on the array - the returned value will be the number of elements in the array −
@array = (1,2,3); print "Size: ",scalar @array,"\n";
The value returned will always be the physical size of the array, not the number of valid elements. You can demonstrate this, and the difference between scalar @array and $#array, using this fragment is as follows −
Example
#!/usr/bin/perl @array = (1,2,3); $array[50] = 4; $size = @array; $max_index = $#array; print "Size: $size\n"; print "Max Index: $max_index\n";
Output
This will produce the following result −
Size: 51 Max Index: 50
There are only four elements in the array that contains information, but the array is 51 elements long, with the highest index of 50.
- Related Articles
- Getting Hash Size in Perl
- Perl Array Variables
- Understanding Perl Array
- Accessing Array Elements in Perl
- Slicing Array Elements in Perl
- Replacing Array Elements in Perl
- How to create Array in Perl?
- Adding and Removing Elements in Perl Array
- Array Size in Lua Programming
- Querying internal array size in MongoDB?
- Reduce Array Size to The Half in C++
- Size of a Three-dimensional array in C#
- Comments in Perl
- Whitespaces in Perl
- Dereferencing in Perl

Advertisements