Suppose we have the following code :
#!usr/bin/perl
use strict ;
use warnings ;
sub print_ele_arr{
my @arr = <STDIN> ;
#print the elements of the array here .
#do something else ..
}
print_ele_arr() ;
but i want to store just 3 elements from the user's input into my @arr array , how to do that , in general how to limit the size of a given array ?
To store just 3 lines, you can use
my $i = 1;
while (defined( my $line = <STDIN>) and $i++ <=3) {
push @arr, $line;
}
As for the second question, what do you mean by limiting the size of an array? You can use an array slice to get just the first three elements of an array:
my @first_three = @arr[0 .. 2];