Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Why doesn't this work?

Reply
Thread Tools

Why doesn't this work?

 
 
David Filmer
Guest
Posts: n/a
 
      01-18-2005
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?

 
Reply With Quote
 
 
 
 
Martin Kissner
Guest
Posts: n/a
 
      01-18-2005
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)
 
Reply With Quote
 
 
 
 
Martin Kissner
Guest
Posts: n/a
 
      01-18-2005
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)
 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      01-18-2005
"David Filmer" <(E-Mail Removed)> wrote in
news:(E-Mail Removed) 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.
 
Reply With Quote
 
Tad McClellan
Guest
Posts: n/a
 
      01-19-2005
David Filmer <(E-Mail Removed)> 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: <(E-Mail Removed)>


--
Tad McClellan SGML consulting
http://www.velocityreviews.com/forums/(E-Mail Removed) Perl programming
Fort Worth, Texas
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-19-2005
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> David Filmer <(E-Mail Removed)> 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
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      01-19-2005
Tad McClellan <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> David Filmer <(E-Mail Removed)> 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

 
Reply With Quote
 
Michele Dondi
Guest
Posts: n/a
 
      01-19-2005
On 18 Jan 2005 14:33:24 -0800, "David Filmer"
<(E-Mail Removed)> 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,
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Cisco 2611 and Cisco 1721 : Why , why , why ????? sam@nospam.org Cisco 10 05-01-2005 08:49 AM
Why, why, why??? =?Utf-8?B?VGltOjouLg==?= ASP .Net 6 01-27-2005 03:35 PM
Why Why Why You HAVE NO IDEA MCSE 31 04-24-2004 06:40 PM



Advertisments