Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Perl Misc (http://www.velocityreviews.com/forums/f67-perl-misc.html)
-   -   Why doesn't this work? (http://www.velocityreviews.com/forums/t890271-why-doesnt-this-work.html)

David Filmer 01-18-2005 10:33 PM

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?


Martin Kissner 01-18-2005 10:38 PM

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)

Martin Kissner 01-18-2005 10:42 PM

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)

A. Sinan Unur 01-18-2005 10:47 PM

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.

Tad McClellan 01-19-2005 01:47 PM

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

Anno Siegel 01-19-2005 02:34 PM

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

Anno Siegel 01-19-2005 02:38 PM

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


Michele Dondi 01-19-2005 10:44 PM

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.


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57