Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

printing element of anonymous array

by perltux (Monk)
on Mar 03, 2018 at 15:31 UTC ( #1210279=perlquestion: print w/replies, xml ) Need Help??

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

How can I rewrite the following two commands in one command, i.e. without having to assign the output of gmtime to an array first?

perl -e 'my @date=gmtime(time); print $date[6]; '

I had hoped the following would work but I get syntax error:

perl -e 'print (gmtime(time))[6]; '

Replies are listed 'Best First'.
Re: printing element of anonymous array
by Corion (Pope) on Mar 03, 2018 at 15:37 UTC

    This is because Perl sees something else, it associates the parentheses with print():

    print(gmtime(time)) [6];

    This makes no sense to Perl.

    To fix this, you need one more set of parentheses:

    print( (gmtime(time))[6] );
      Many thanks for the quick explanation and solution!
Re: printing element of anonymous array
by Athanasius (Bishop) on Mar 03, 2018 at 15:43 UTC

    Hello perltux,

    FTR, you can also disambiguate the syntax with a plus sign:

    1:41 >perl -e "print (gmtime(time))[6];" syntax error at -e line 1, near ")[" Execution of -e aborted due to compilation errors. 1:41 >perl -e "print ((gmtime(time))[6]);" 6 1:41 >perl -e "print +(gmtime(time))[6];" 6 1:41 >

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

Re: printing element of anonymous array
by dave_the_m (Monsignor) on Mar 03, 2018 at 15:39 UTC
    The outer parentheses are being grabbed by the print function. You need
    perl -e 'print ((gmtime(time))[6]);'

    PS - technically, it's a list, not an anonymous array, being returned by gmtime.

    Dave.

Re: printing element of anonymous array
by poj (Abbot) on Mar 03, 2018 at 15:48 UTC

    alternatively

    perl -e 'print [gmtime(time)]->[6];'
Re: printing element of anonymous array
by 1nickt (Abbot) on Mar 03, 2018 at 20:51 UTC

    Using the core Perl module Time::Piece (which overrides the built-in functions localtime and gmtime, providing functions that return objects):

    use Time::Piece;
    print gmtime->wday;


    The way forward always starts with a minimal test.

        True, but there's a reason Time::Piece was placed in core, and it ain't because Time::gmtime had feature bloat.

        Besides the aforementioned wday(), which provides a one-indexed numerical value for the day of the week, Time::Piece also provides _wday() and day_of_week() for getting the zero-indexed value, as well as fullday() to get the full name of the day, and day() and wdayname() for an abbreviated version of the name.


        The way forward always starts with a minimal test.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (6)
As of 2020-01-20 09:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?