Perl allows ordered collections of values called lists to be stored in array variables. Lists are sequences of scalar values enclosed in parentheses that can contain any data type. Array variables start with '@' and are used to store lists, with each element assigned an index starting from 0. Individual array elements are accessed using the array name and index in the format $array_name[index]. New elements can be added to arrays and existing elements can be updated by assigning values to indices.
Perl allows ordered collections of values called lists to be stored in array variables. Lists are sequences of scalar values enclosed in parentheses that can contain any data type. Array variables start with '@' and are used to store lists, with each element assigned an index starting from 0. Individual array elements are accessed using the array name and index in the format $array_name[index]. New elements can be added to arrays and existing elements can be updated by assigning values to indices.
Perl allows ordered collections of values called lists to be stored in array variables. Lists are sequences of scalar values enclosed in parentheses that can contain any data type. Array variables start with '@' and are used to store lists, with each element assigned an index starting from 0. Individual array elements are accessed using the array name and index in the format $array_name[index]. New elements can be added to arrays and existing elements can be updated by assigning values to indices.
Perl allows to define an ordered collection of values called
“lists”. Ex: (10,20,30,40); This ordered collection of elements can be stored in the variables called “arrays”. List is a sequence of scalar values enclosed in parantheses. Ex: (10,”rama”,12.44,’A’); List can be of any length. We can create empty lists also as “( )” (43.2) and 43.2 are not same. The first one is a single element list and second one is a scalar value. We can have scalars also as a member of the list as: Ex: (23,$a, 34.2) We can have value of expressions also as members of the list. Ex: (17, $a>$b) (90,$a+$b) Storing lists in array variables To store lists, Perl uses special variables called “array variables” (or) “arrays”. Name of an array starts with ‘@’ and the 2nd character can be an alphabet. Digits, underscores are also allowed. Ex: @arr=(1,2,3); @my_arr=(“ Jones”,123);
Accessing the array elements
Array elements have indices associated with them.
Index always starts from 0, by default. Array elements are referred by their index values as: $array_name[index] Ex: @list=(1,2,34,4,23); print $list[0]; print $list[1]; Even though ‘list’ is an array, while referring independent array elements, we use ‘$’ only.
If we try to access an element which is not existing, we get
“null” (or) “undef” value. Ex: @list=(1,2,34,4,23); print $list[10]; à undef Ex: $x=$list[6]; Now, $x holds ‘undef’ value, as there is no element defined in the array with the index 6. Assigning a new value to an array Ex: Suppose the array is : @arr=(10,20,30,40,50); To add new element to the array, $arr[5]=100; Now, our array will be: @arr=(10,20,30,40,50,100); Ex: $arr[10]=200; Now, the array is (10,20,30,40,50,100,”” ,””,””,””,200);
Ex: print all array elements
printing using foreach @arr=(10,20,30,40); foreach $i(@arr) { print "$arr[$i]\t"; }
List range operator(..)
Used to store consecutive numbers. Ex: @arr=(1,2,3,4,5,6,7,8,9,10); @arr=(1..10); We can use ranges as a part of the list as: @arr=(10,20,5..15,90,78);