jonam has asked for the wisdom of the Perl Monks concerning the following question:

How can I skip the first two return values of the function(which returns an array)?

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.

Replies are listed 'Best First'.
Re: Skip return values
by jwkrahn (Abbot) on Apr 03, 2009 at 06:24 UTC
    my ( undef, undef, $c, $d ) = sample();
Re: Skip return values
by lakshmananindia (Chaplain) on Apr 03, 2009 at 05:25 UTC

    You can do that as follows

    my $c=(&sample)[2]; print $c;
    --Lakshmanan G.

    The great pleasure in my life is doing what people say you cannot do.


Re: Skip return values
by vinoth.ree (Monsignor) on Apr 03, 2009 at 06:26 UTC

    If you return array reference like my $arr=[1,2,3,4]; return $arr. Then you can achieve what you expect as follows

    my $c=@{&sample}[2];
      And, if returning an array reference, yet another way:
      >perl -wMstrict -le "sub sample { return [1, 2, 3, 4]; } my $c = sample()->[2]; print $c; " 3
      BTW: The  &sample form of subroutine invocation is deprecated.
        You are passing @ARGV to sample. Thats why the syntax &subroutine is bad TM. Its not just cosmetically ugly.

        print+qq(\L@{[ref\&@]}@{['@'x7^'!#2/"!4']});