Beefy Boxes and Bandwidth Generously Provided by pair Networks
XP is just a number

Looking for syntactic shortcut

by mrd (Beadle)
on Jul 19, 2005 at 12:48 UTC ( #476062=perlquestion: print w/replies, xml ) Need Help??

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

Dear monks,

I'm looking for a shortcut to extract a specific element out of an array without using a temporary variable. The array is returned by a function and I only need a particular element of that.

Since a line of code is worth 2000 lines of plain (gibberish) text (of mine), here is what I do:

@tmp = function($param); $elem = $tmp[5]
Only I want to do it shorter. Something like:
$elem = (function($param))[5]
which obviously doesn't work.



PS: I could wrap function() in another function that returns the element I want from a local array. But that's just too much work :)

PPS: I'm sure it's been discussed before but I can't seem to come up with an adequate search expression.

Replies are listed 'Best First'.
Re: Looking for syntactic shortcut
by merlyn (Sage) on Jul 19, 2005 at 12:52 UTC
    Only I want to do it shorter. Something like:
    $elem = (function($param))[5]
    which obviously doesn't work.
    No falser words have been spoken. {grin}

    Did you try it? It works just fine. It's precisely why I invented that notation in the first place (for Perl version 3).

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

      Ummm .... yes I did:
      # perl -e "print localtime()[5]" syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.
        I dont know about print, but assignment to a scalar works fine:
        perl -e 'print $a=(localtime())[5]' 105

        Update: Or, following BrowserUK:

        perl -e 'print +(localtime())[5]' 105

        Update: But not (in response to socketdave):

        perl -e 'print (localtime())[5]' syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors.
        It seems we are hitting the finer points in the parser here...
        look closer at Merlyn's example... you need '(localtime())[5]' instead of 'localtime()[5]'...

        Note that in your original post you had parentheses around the subroutine, these is also a slight gotcha in this example if you do:

        perl -e'print (localtime())[5]'
        as the parser takes the outer parentheses to be the argument list of the print which obviously can't be subscripted meaningfully, in this case you will want to use the no-op unary plus to disambiguate the context of the subscript:
        perl -e'print +(localtime())[5]'


        which should read:
        print +(localtime)[5]
        (note the + before the opening bracet, to prevent print from thinking it's his. When assigning to a variable, you say:
        my $year = (localtime)[5]
        just as you state in your own post :)


        That's not a very good test. Here's a very small program that does what you say you want:

        #!/usr/bin/perl use warnings; use strict; sub retarray { my $param = shift; my @array=(1,2,3,4,5,56); return @array; } my $param = 2; my $val = (retarray($param))[3]; print "val is $val\n";
        perl -e "print((localtime)[5])"
        perl -e "print+(localtime)[5]"
        Quiz: can you explain why:
        perl -e "print (localtime)[5]"
        gives a syntax error?
Re: Looking for syntactic shortcut
by BrowserUk (Pope) on Jul 19, 2005 at 12:52 UTC

    What you have should work fine. What error are you getting?

    sub func{ return 0 .. 10 };; print +(func())[5];; 5

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    The "good enough" maybe good enough for the now, and perfection maybe unobtainable, but that should not preclude us from striving for perfection, when time, circumstance or desire allow.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://476062]
Approved by marto
Front-paged by Thilosophy
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others exploiting the Monastery: (4)
As of 2019-10-16 04:29 GMT
Find Nodes?
    Voting Booth?