Consider the following example
#! /bin/perl
use strict;
sub sample
{
my @arr=(1,2,3,4);
return @arr;
}
my ($a,$b,$c,$d) = &sample;
print "$a\n$b\n$c\n$d\n";
In the above code, How can I get the $c without using the
arguments such as $a,$b.
I don't want to use any array to get the return values.
Answers were Sorted based on User's Feedback
Answer / guest
Try with the following program.
[code]
sub sample
{
my @arr=(1,2,3,4);
return @arr;
}
my $c=(&sample)[2];
print $c;
Variable 'c' will contain the value '3'.
| Is This Answer Correct ? | 4 Yes | 0 No |
Answer / guest
# Here is another solution
use strict;
sub sample {
my @arr = (1,2,3,4);
return \@arr; # return the array reference
}
my $aref = &sample;
print $aref->[2];
| Is This Answer Correct ? | 0 Yes | 0 No |
What does 'do' statement do in perl?
List all the features of perl programming?
What does last statement do in perl?
What is the peculiarity of returning values by subroutines in perl?
Why do you use only Perl when there a lot of more languages available in market like C, Java?
What is 'commit' command in perl?
Mention the difference between die and exit in Perl?
How to replace perl array elements?
What is the use of -w, -t and strict in Perl?
How do you turn on the perl warnings?
How do I sort a hash by the hash key?
how to search a unique pattern in a file by using perl hash map function ??? plz answer me