Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

While loop with an "OR" condition

Tags:

perl

Below is a code snippet that asks for user input, and if the input is not either "csv" or "newline", the while loop is called.

For line 5, what is the correct syntax for the while loop where it attempts to match $format to either "csv" or "newline"? Currently it is only seems to be matching "csv".

1 # Request output format
2 print "Format of email addresses required (csv|newline): ";
3 $format = <>;
4 chop($format);
5 while ($format ne ("csv"||"newline")) {
6   print "Invalid format. Enter in csv or newline: ";
7   $format = <>;
8   chop($format);
9 }
like image 780
kurotsuki Avatar asked Feb 12 '26 15:02

kurotsuki


1 Answers

If using Perl >= v5.10, the closest working example to what you tried is:

while ( not $format ~~ ["csv", "newline"] ) {

Otherwise, both of rob mayoff's solutions will work just fine.

like image 153
Richard Simões Avatar answered Feb 15 '26 12:02

Richard Simões