![]() |
Why doesn't this work?
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? |
Re: Why doesn't this work?
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) |
Re: Why doesn't this work?
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) |
Re: Why doesn't this work?
"David Filmer" <IneverReadAnythingSentToMe@hotmail.com> wrote in
news:1106087604.564825.130160@c13g2000cwb.googlegr oups.com: > 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. |
Re: Why doesn't this work?
David Filmer <IneverReadAnythingSentToMe@hotmail.com> 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: <slrnatq036.2q2.tadmc@magna.augustmail.com> -- Tad McClellan SGML consulting tadmc@augustmail.com Perl programming Fort Worth, Texas |
Re: Why doesn't this work?
Tad McClellan <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
> David Filmer <IneverReadAnythingSentToMe@hotmail.com> 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 |
Re: Why doesn't this work?
Tad McClellan <tadmc@augustmail.com> wrote in comp.lang.perl.misc:
> David Filmer <IneverReadAnythingSentToMe@hotmail.com> 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 |
Re: Why doesn't this work?
On 18 Jan 2005 14:33:24 -0800, "David Filmer"
<IneverReadAnythingSentToMe@hotmail.com> 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, |
| All times are GMT. The time now is 11:30 AM. |
Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.