Fendo has asked for the wisdom of the Perl Monks concerning the following question:
I've been trying to print a character a certain number of times, if a certain condition is met. For the printing, I use an “until” loop. For the condition, I would like to use the conditional (ternary) operator. However, this apparently results in a syntax error. Below is example code which produces the error. My intention was to print the equals sign ten times instead.
#!/usr/bin/perl use strict; use warnings; my $count = 0; ( defined $count ) ? ( print "=" until ( $count ++ == 10 ) ) : ( print "Undefined count.\n" );
This results in the error: syntax error at ./example.pl line 8, near ""=" until". I have tried using a different form for the loop:
( defined $count ) ? ( until ( $count ++ == 10 ) { print "=" } ) : ( print "Undefined count.\n" );
However, that results in a similar error: syntax error at ./example.pl line 8, near "( until". When I try to achieve this using “if” and “else”, it works very well, using this code:
Is it possible to use such constructions within the conditional (ternary) operator, as I've been trying to do? And if yes, how may I do so correctly? Thank you in advance!if ( defined $count ) { print "=" until ( $count ++ == 10 ); } else { print "Undefined count.\n"; }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Compound statements within the conditional (ternary) operator (updated)
by AnomalousMonk (Archbishop) on Apr 14, 2018 at 12:27 UTC | |
|
Re: Compound statements within the conditional (ternary) operator
by roboticus (Chancellor) on Apr 14, 2018 at 19:52 UTC | |
by AnomalousMonk (Archbishop) on Apr 14, 2018 at 23:48 UTC | |
by roboticus (Chancellor) on Apr 15, 2018 at 02:57 UTC | |
|
Re: Compound statements within the conditional (ternary) operator
by mr_ron (Deacon) on Apr 14, 2018 at 13:02 UTC |