473,473 Members | 2,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Inverted syntax for an if conditional

I have some information that states that the if conditional can be be inverted
from the traditional syntax

if (EXPRESSION) BLOCK

to an alternative syntax:

if BLOCK (EXPRESSION);

I have a simple line of code:

if ($guess == 6) { print 'Wow! Lucky Guess!'; }

However, when I try to invert this, I get a syntax error:

{ print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error

Why does this not work?

The example is academic, and I don't intend to code with the inverted syntax.
I am just trying to get an understanding for the purpose of producing
documentation.

Thanks in advance to anyone who can help.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Apr 22 '06 #1
7 8847
Mark Hobley wrote:
I have some information that states that the if conditional can be be
inverted from the traditional syntax

if (EXPRESSION) BLOCK

to an alternative syntax:

if BLOCK (EXPRESSION);
Did you mean
BLOCK if (EXPRESSION);

Anyway, both are wrong. Why don't you check the documenation?
From "perldoc perlsyn":

Any simple statement may optionally be followed by a *SINGLE* modifier,
just before the terminating semicolon (or block ending). The possible
modifiers are:
if EXPR

I can only guess that this is what you were looking for.
However, when I try to invert this, I get a syntax error:
{ print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error


Because a block is not a simple statement. Did you try
print 'Wow! Lucky Guess!' if ($guess == 6);

jue
Apr 22 '06 #2
On 2006-04-22, Mark Hobley <ma********@hotpop.deletethisbit.com> wrote:
I have some information that states that the if conditional can be be inverted
from the traditional syntax

if (EXPRESSION) BLOCK

to an alternative syntax:

if BLOCK (EXPRESSION);

I have a simple line of code:

if ($guess == 6) { print 'Wow! Lucky Guess!'; }

However, when I try to invert this, I get a syntax error:

{ print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error

Why does this not work?

The example is academic, and I don't intend to code with the inverted syntax.
I am just trying to get an understanding for the purpose of producing
documentation.


This works for me:

#!/usr/bin/perl
my $guess=6;
print "Wow! Lucky guess!\n" if ( $guess == 6 ) ;

(followups set)
Justin.

--
Justin C, by the sea.
Apr 22 '06 #3
In alt.perl Mark Hobley <ma********@hotpop.deletethisbit.com> wrote:

{ print 'Wow! Lucky Guess!'; } if ($guess == 6); # Syntax error

Why does this not work?


I have also discovered some more weird behaviour, this time using the syntax:

STATEMENT [, STATEMENT ], if EXPRESSION;

If I use:

print 'I should be so lucky!', print 'Chucky!', print 'Mucky!', print
'Clucky!', if ($guess == 6);

The statements run in reverse order, and I get number ones inserted in the
output:

Clucky!Mucky!1Chucky!1I should be so lucky!1

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Apr 22 '06 #4
In alt.perl "Jürgen Exner" <ju******@hotmail.com> wrote:
Mark Hobley wrote: Did you mean
BLOCK if (EXPRESSION);
Oops, yes I did. That was a typing error.
Anyway, both are wrong. Why don't you check the documenation?
From "perldoc perlsyn":

Any simple statement may optionally be followed by a *SINGLE* modifier,
just before the terminating semicolon (or block ending). The possible
modifiers are:
if EXPR

Because a block is not a simple statement. Did you try
print 'Wow! Lucky Guess!' if ($guess == 6);


Yeah that works.

A professional programming guide tells me that the following forms of if
statement are legal in Perl:

STATEMENT if EXPRESSION;

STATEMENT, STATEMENT ... if EXPRESSION;

BLOCK if EXPRESSION;

Presumably there is a mistake in the text, and BLOCK if EXPRESSION should be
omitted from this list, because STATEMENT if EXPRESSION works, but
BLOCK if EXPRESSION appears not to.

Regards,

Mark.

--
Mark Hobley
393 Quinton Road West
QUINTON
Birmingham
B32 1QE

Telephone: (0121) 247 1596
International: 0044 121 247 1596

Email: markhobley at hotpop dot donottypethisbit com

http://markhobley.yi.org/

Apr 22 '06 #5
Mark Hobley wrote:
I have also discovered some more weird behaviour,
print 'I should be so lucky!', print 'Chucky!', print 'Mucky!', print
'Clucky!', if ($guess == 6);

The statements run in reverse order, and I get number ones inserted
in the output:

Clucky!Mucky!1Chucky!1I should be so lucky!1


Nothing weird at all.
If you add explicit paranthesis then it's obvious what's going on:

print ('I should be so lucky!',
(print 'Chucky!',
(print 'Mucky!',
print ('Clucky!')
)
)
)

The injected digits '1' are just the return value 'true' of the inner
print() statements, e.g. for the outmost you will get eventually

print ('I should be so lucky!', 1)

I suggest you read the documentation for the functions that you are using.
"perldoc -f print":

print Prints a string or a list of strings. Returns true if
successful.
jue
Apr 22 '06 #6
>>>>> "Mark" == Mark Hobley <ma********@hotpop.deletethisbit.com> writes:

Mark> A professional programming guide tells me that the following forms of if
Mark> statement are legal in Perl:

Mark> STATEMENT if EXPRESSION;

Mark> STATEMENT, STATEMENT ... if EXPRESSION;

Mark> BLOCK if EXPRESSION;

Mark> Presumably there is a mistake in the text, and BLOCK if EXPRESSION should be
Mark> omitted from this list, because STATEMENT if EXPRESSION works, but
Mark> BLOCK if EXPRESSION appears not to.

That's really wrong.

It's:

EXPRESSION if EXPRESSION;

or

if (EXPRESSION) BLOCK

where BLOCK is:

{ STATEMENT STATEMENT ... STATEMENT }

and STATEMENT is:

EXPRESSION;

and many other things.

--
Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
<me****@stonehenge.com> <URL:http://www.stonehenge.com/merlyn/>
Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
*** Posted via a free Usenet account from http://www.teranews.com ***
Apr 22 '06 #7
Mark Hobley wrote:

A professional programming guide tells me that the following forms of if
statement are legal in Perl:

STATEMENT if EXPRESSION;

STATEMENT, STATEMENT ... if EXPRESSION;

BLOCK if EXPRESSION;

Presumably there is a mistake in the text, and BLOCK if EXPRESSION should be
omitted from this list, because STATEMENT if EXPRESSION works, but
BLOCK if EXPRESSION appears not to.


do BLOCK if EXPRESSION
perldoc -f do

Regards,
Christoph
--

perl -e "print scalar reverse q/ed*******@ergn.l.hc/"
Apr 22 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Kiki Novak | last post by:
Hi, I'm an Austrian writer living in Montpezat (South France), and I'm currently busy converting some of my writings from LaTeX to HTML to publish them on the Internet. The novel I'm working...
15
by: Andy Fish | last post by:
Looking at an HTML document recently, I found this syntax which was completely new to me <!> <style>...</style> <!> it looks to be some kind of conditional logic but it's not in a javascript...
177
by: C# Learner | last post by:
Why is C syntax so uneasy on the eye? In its day, was it _really_ designed by snobby programmers to scare away potential "n00bs"? If so, and after 50+ years of programming research, why are...
12
by: Brad Baker | last post by:
I am trying to write a simple ASP.net/C# page which allows users to select some values and produce a report based on a SQL query. I have a self posting dropdown form which allows users to select...
5
by: vd12005 | last post by:
Hello, While playing to write an inverted index (see: http://en.wikipedia.org/wiki/Inverted_index), i run out of memory with a classic dict, (i have thousand of documents and millions of terms,...
2
by: weird0 | last post by:
I have to create a string that contains inverted commas inside it. How can i do that in c#? As follows:- "AT+CMGF=1" string temp=""AT+CMGF=1""; // not valid
1
by: Geosondaman | last post by:
#include <stdio.h> #include <stdlib.h> int main(int argc, char *argv) { int i=0, j; while (++i<10) { j=9; while (++j<=i) { printf("*");
1
by: Shashank tiwari | last post by:
Hi I am trying Putting single inverted comma before a digit in Excel and a single inverted comma after the number and a comma to follow. Eg. '234567', Can anybody tell us how to do it for 7000...
13
by: Neal Becker | last post by:
In hindsight, I am disappointed with the choice of conditional syntax. I know it's too late to change. The problem is y = some thing or other if x else something_else When scanning this my...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.