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.
Answer Posted / 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 |
Post New Answer View All Answers
How to turn on Perl warnings? Why is that important?
What happens to objects lost in "unreachable" memory, such as the object returned by Ob->new() in `{ my $ap; $ap = [ Ob->new(), $ap ]; }' ?
Explain perl. When do you use perl for programming?
What is a perl references?
What is posix in perl?
what is the function of Return Value?
What is the difference between perl array and perl hash?
What are the advantages and disadvantages of perl language?
How can we create perl programs in unix, windows nt, macintosh and os/2 ?
What is it meants by '$_'?
Explain the difference between declarations of 'my' and 'local' variable scope in perl?
Which functions in Perl allows you to include a module file or a module and what is the difference between them?
What is subroutine in perl?
How to determine strings length in perl?
Explain the functioning of conditional structures in perl.