Why doesn't this work?

D

David Filmer

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?
 
M

Martin Kissner

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
 
M

Martin Kissner

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
 
A

A. Sinan Unur

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.
 
T

Tad McClellan

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: <[email protected]>
 
A

Anno Siegel

Tad McClellan said:
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
 
A

Anno Siegel

Tad McClellan said:
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top