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"; }
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |