Re: Syntax error - beginner question
by dws (Chancellor) on Jan 11, 2004 at 07:16 UTC
|
Perl doesn't support expressions like
if (9 < $X < 100)
Instead, you'll need to phrase it like
if (9 < $X && $X < 100)
Or, to be clearer on your intent
if ( $X >=10 && $X <=99 )
though this may be a matter of style.
| [reply] [d/l] [select] |
|
|
| [reply] |
|
|
The precedence rules are usually sensible. It's fairly common to combine comparisons with logical operators; hence comparisons have higher precedence.
| [reply] |
Re: Syntax error - beginner question
by davido (Cardinal) on Jan 11, 2004 at 07:17 UTC
|
The syntax error you're getting is accurate:
You cannot do a double-comparison as a single expression. The answer is to use the logical and operator to chain two expressions.
if ( $X > 9 and $X < 100 ) {
$Y = $X + 1900;
} elsif ( $X >= 0 and $X < 10 ) {
$Y = $X + 2000;
}
By the way, that's just one way to do it.
| [reply] [d/l] [select] |
Re: Syntax error - beginner question
by ysth (Canon) on Jan 11, 2004 at 07:54 UTC
|
You learn something every day. I would have assumed a < b < c would have compared the result of a < b (i.e. 0 or 1 for true or false) with c. (I have a tendency to assume left-associativity except with what I know to be right-associative.) It's interesting to see which operators perlop says can't be chained like that (the ones listed as nonassociative).
Seems a little suboptimal to handle it with yacc, rather than something that could give a better error message. | [reply] |
Re: Syntax error - beginner question
by TomDLux (Vicar) on Jan 11, 2004 at 15:21 UTC
|
Range comparisons are practically unknown among programming languages. As far as I know, Perl 6 will be the first widely used language to allow range checking.
Older languages use the excuse of implementation to forbid something which is a natural mathematical statement. Comparisons like < return a boolean value, a truth value: Is 9 less than $X? yes or no?. Languages such as C would then use the result of that comprison in stage two: Is (9<$X) less than 100?. Obviously unlikely to be what you want, so Perl detects and reports the error.
By optimizing your comparisons a little, you can eliminate the need for range testing:
if ( $X < 0 ) {
handle_X_under_0();
} elsif ( $x < 10 ) {
$Y = $X + 2000;
} elsif ( $X < 100 ) {
$Y = $X + 1900;
} else {
handle_X_over_100();
}
--
TTTATCGGTCGTTATATAGATGTTTGCA
| [reply] [d/l] |
Re: Syntax error - beginner question
by abmodi (Initiate) on Jan 11, 2004 at 13:37 UTC
|
you cannot use code like a < $X < bin Perl ..
HMWTDI :
$Y = ($X + 2000) if ($X >=0 && $X < 10) ;
$Y = ($X + 1900) if ($X >=10 && $X < 100) ;
Regards
Abhinav | [reply] [d/l] [select] |
Re: Syntax error - beginner question
by Aristotle (Chancellor) on Jan 11, 2004 at 15:06 UTC
|
Written with a ternary it would be easier to understand and easier to update.
$Y += ($X < 10) ? 2000 : 1900;
Even at such tiny scale, Once And Only Once is beneficial.
Makeshifts last the longest.
| [reply] [d/l] |
|
|
Ay yi yi...as a beginner at Perl that just makes my brain hurt. ;-)
Can someone parse that out, or give me a link to where it's explained. Thanks
| [reply] |
|
|
This all stuff used in many languages and not specific to Perl.
The ternary operator works in the form of $condition_expr ? $true_expr : $false_expr. The conditional expression (here: ($X < 10)) is evaluated first. If it tests true, the $true_expr is then evaluated and its result is returned. Likewise with $false_expr if the conditional expression evaluated to false.
Then you have a shortcut operator $foo op= $bar;, where op stands for a mathematical, bitwise, or logical operator - here +. It is a shortcut for $foo = $foo op $bar;. So $foo += $bar; is a concise way to say "increase $foo by $bar".
So a sort-of token-by-token translation of the expression in my post to English would read something like "$Y is increased by either (if $X is less than 10) 2000 or by 1900 (otherwise)".
Obviously the first time you see such a construct it'll look strange. And it certainly isn't the point to use them wherever possible. Brevity is not a goal for its own sake. But assuming familiarity with such constructs, readability is maximized by choosing the briefest possible form that follows the structure of a natural language sentence describing the same thought. In this case this is statement is closest to how I'd explain the intent: "add the century depending on the year". It expresses exactly that in a straightforward manner: there's one condition, one addition/assignment, and the numbers, without any artificial extra verbiage.
Makeshifts last the longest.
| [reply] |
|
|
I know that feeling! It's a common brain-response when getting questions answered here. I highly recommend getting a copy of Learning Perl. Basic stuff like this is covered well with many exercises to give you a feel for how to use the language.
-Theo-
(so many nodes and so little time ... )
| [reply] |