Why doesn't this work?

Discussion in 'Perl Misc' started by David Filmer, Jan 18, 2005.

  1. David Filmer

    David Filmer Guest

    Why can't I say:

    print (gmtime(time))[2];

    I expect that to print the hour (localized for GMT). But it tells me
    this is a syntax error. But I can do this:

    @foo = gmtime(time);
    print @foo[2];

    What's wrong with the first syntax?
    David Filmer, Jan 18, 2005
    #1
    1. Advertising

  2. David Filmer wrote :
    > Why can't I say:
    >
    > print (gmtime(time))[2];
    >
    > I expect that to print the hour (localized for GMT). But it tells me
    > this is a syntax error. But I can do this:
    >
    > @foo = gmtime(time);
    > print @foo[2];


    or this:
    print my $temp=(gmtime(time))[2];

    HTH
    Martin

    --
    Epur Si Muove (Gallileo Gallilei)
    Martin Kissner, Jan 18, 2005
    #2
    1. Advertising

  3. David Filmer wrote :
    > Why can't I say:
    >
    > print (gmtime(time))[2];
    >
    > I expect that to print the hour (localized for GMT). But it tells me
    > this is a syntax error. But I can do this:
    >
    > @foo = gmtime(time);
    > print @foo[2];


    or this:
    print my $temp=(gmtime(time))[2];
    or this:
    perl -e 'print do {(gmtime(time))[2]}'

    HTH
    Martin

    --
    Epur Si Muove (Gallileo Gallilei)
    Martin Kissner, Jan 18, 2005
    #3
  4. "David Filmer" <> wrote in
    news::

    > Why can't I say:
    >
    > print (gmtime(time))[2];


    Well, if you had enabled warnings, you would have seen useful
    information:

    D:\> perl -we "print (gmtime(time))[2]"
    print (...) interpreted as function at -e line 1.
    syntax error at -e line 1, near ")["
    Execution of -e aborted due to compilation errors.

    OTOH:

    D:\> perl -we "print +(gmtime(time))[2]"
    22

    Also, if you had checked the documentation for for print, you would have
    read:

    D:\> perldoc -f print

    ... Because print takes a
    LIST, anything in the LIST is evaluated in list context, and any
    subroutine that you call will have one or more of its
    expressions evaluated in list context. Also be careful not to
    follow the print keyword with a left parenthesis unless you want
    the corresponding right parenthesis to terminate the arguments
    to the print--interpose a "+" or put parentheses around all the
    arguments.

    ....

    > @foo = gmtime(time);
    > print @foo[2];
    >
    > What's wrong with the first syntax?


    Actually, the @foo[2] syntax is wrong as well. You are selecting a slice
    consisting of the third element of foo. You should use $foo[2] access
    the third element of an array foo.

    You should also read the posting guidelines for this group.

    Sinan.
    A. Sinan Unur, Jan 18, 2005
    #4
  5. David Filmer <> wrote:


    You should always enable warnings when developing Perl code!


    > Why can't I say:
    >
    > print (gmtime(time))[2];



    Because the parser sees a function call:

    print (gmtime(time))

    followed by

    [2]

    and gets confused.


    > What's wrong with the first syntax?



    You need to help the parser parse when you want an open paren
    that follows a function name to NOT enclose the function's
    argument list.

    print ((gmtime(time))[2]); # use parens around arg list
    or
    print +(gmtime(time))[2]; # no paren following function name


    See also the discussion in:

    Message-Id: <>


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
    Tad McClellan, Jan 19, 2005
    #5
  6. David Filmer

    Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > David Filmer <> wrote:
    >
    > You should always enable warnings when developing Perl code!


    You forgot "Please put the subject of your message in the Subject of your
    message" :)

    "Why doesn't this work?" is an especially egregious example
    of a thoughtless and useless Subject.

    The only part of a posting that *every* potential replier is going to see
    is the Subject line, which is thus a key feature for a posting's success
    or failure. It pays to spend a moment to make it informative and appealing.
    Unexperienced posters often throw in the first thing that comes to mind,
    which is usually neither.

    One good rule is: The Subject should be about the question you are asking
    the group, not the question you are asking yourself.

    Anno
    Anno Siegel, Jan 19, 2005
    #6
  7. David Filmer

    Anno Siegel Guest

    Tad McClellan <> wrote in comp.lang.perl.misc:
    > David Filmer <> wrote:
    >
    > You should always enable warnings when developing Perl code!


    You forgot "Please put the subject of your message in the Subject of your
    message" :)

    "Why doesn't this work?" is an especially egregious example of a thoughtless
    and useless Subject.

    The only part of a posting that *every* potential replier is going to see
    is the Subject line, which is thus a key feature for a postings success
    or failure. It pays to spend a moment to make it informative and appealing.
    Unexperienced posters often throw in the first thing that comes to mind,
    which is usually neither.

    One good rule is: The Subject should be about the question you are asking
    the group, not the question you are asking yourself.

    Anno
    Anno Siegel, Jan 19, 2005
    #7
  8. On 18 Jan 2005 14:33:24 -0800, "David Filmer"
    <> wrote:

    >Subject: Why doesn't this work?


    Hint: choose better subject lines.

    >print (gmtime(time))[2];


    print +(gmtime(time))[2];
    # print +(gmtime time)[2];


    Michele
    --
    {$_=pack'B8'x25,unpack'A8'x32,$a^=sub{pop^pop}->(map substr
    (($a||=join'',map--$|x$_,(unpack'w',unpack'u','G^<R<Y]*YB='
    ..'KYU;*EVH[.FHF2W+#"\Z*5TI/ER<Z`S(G.DZZ9OX0Z')=~/./g)x2,$_,
    256),7,249);s/[^\w,]/ /g;$ \=/^J/?$/:"\r";print,redo}#JAPH,
    Michele Dondi, Jan 19, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. David Prowak

    Why oh why doesn't my data view work?

    David Prowak, Jan 30, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    725
    Alvin Bruney [MVP]
    Jan 30, 2004
  2. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  3. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,766
    Smokey Grindel
    Dec 2, 2006
  4. Sara
    Replies:
    6
    Views:
    249
    John W. Krahn
    Apr 12, 2004
  5. PerlFAQ Server
    Replies:
    0
    Views:
    246
    PerlFAQ Server
    Apr 26, 2011
Loading...

Share This Page