Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

list returned from function

by hotshot (Prior)
on Jan 17, 2002 at 21:09 UTC ( #139547=perlquestion: print w/ replies, xml ) Need Help??
hotshot has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys !

Is there a short way to get the x entry of an array returned from a function?

example:
# if I have the function myFunc() that returns an list # I would like to get somehow the 5 element of the list returned: $elem = myFunc()[5]; # of course that doesn't work
anyone?

Hotshot

Comment on list returned from function
Download Code
Re: list returned from function
by japhy (Canon) on Jan 17, 2002 at 21:12 UTC
    You need an extra layer of parens. $elem = (myFunc())[5];

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

      In addition to japhy's excellent answer, I'd like to just point out what this is doing for anyone who might not quite understand why this works as it does.

      (my_func()) forces the output of this function into list context as well as groups it for the [5] we're about to append. Now that [5] takes a slice of the list.

      Here's a simple example
      sub foo { return qw(a b c d) } ( foo() )[1,3]; # Is the same as: ('a', 'b', 'c', 'd')[1,3]; # And effectively the same as @a = foo(); @a[1,3];
      You can read more about slicing in the book Effective Perl Programming (a *must* read =)
Re: list returned from function
by vagnerr (Prior) on Jan 18, 2002 at 01:35 UTC
    You're nearly there...
    $elem = (myFunc())[5];
    will do it
Re: list returned from function
by George_Sherston (Vicar) on Jan 18, 2002 at 02:43 UTC
    The preceding answers assume that MyFunc() returns an array. If it returns an array reference, as it might, you need
    sub MyFunc { my @ary = qw(a b c d e f g h i j); return \@ary; } my $foo = ${MyFunc()}[4];


    George Sherston
      Nothing wrong with the above. But I prefer:
      my $foo = (MyFunc())->[4];
      /prakash
Re: list returned from function
by theorbtwo (Prior) on Jan 18, 2002 at 07:04 UTC

    Above and beyond the exelent answers above, I'd just like to note that in newer perls, that works exactly as you wrote it -- it's special-cased in the source. I think the change is as of 5.6, but I can't seem to find it in the perldelta* manpages.

    Thanks,
    James Mastros,
    Just Another Perl Scribe

Re: list returned from function
by thunders (Priest) on Jan 18, 2002 at 20:37 UTC
    As some of the examples above illustrate, always remember that an array index starts at zero. When you asked for the "5 element" do you mean the "5th element"?
    $elem = (myFunc())[4]; #fifth element $elem = (myFunc())[5]; #sixth element
    It may have just been a typo, but I thought I'd point it out
Re: list returned from function
by Steve_p (Priest) on Jan 19, 2002 at 02:25 UTC
    I would try:
    $elem = (myFunc())[5];
    This is what works for me when I need a single field from localtime. For example,
    $day = (localtime)[6];

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://139547]
Approved by root
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (11)
As of 2015-05-06 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    In my home, the TV remote control is ...









    Results (151 votes), past polls