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 |
how to find a substring in a string without using substr built in functions, and print the substring found
In Perl, what is grep function used for?
There is no strict data types in perl unlike in other high level languages like Java so wouldn't that be a problem if a code in perl is to be a written by a big team of 20+ members ?"
Explain subroutine in perl?
How to sort dates in Perl ?
How do I pass a command line argument in perl?
Explain lists in perl?
How can you replace the characters from a string and save the number of replacements?
What is the easiest way to download the contents of a URL with Perl?
Hi, I am a accountant. I am preparing a balance sheet but because of staff shortage and time pressures I cant complete it on time. There is lot of common data with last years which I need not retype and I can manage by editing last year’s balance sheet ? Is their any software on net where I can do this easily??
Suppose an array contains @arraycontent=(‘ab’, ‘cd’, ‘ef’, ‘gh’). How to print all the contents of the given array?
How do I print the entire contents of an array with Perl?