Reputation: 4777
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 }
Upvotes: 2
Views: 8807
Reputation: 29679
You should use while (($format ne "csv") && ($format ne "newline")) { }
.
In your code, the result of "csv"||"newline"
is "cvs"
; your code is then equivalent to while ($format ne "csv") { }
.
Upvotes: 1
Reputation: 385580
while ($format ne 'csv' && $format ne 'newline') {
...
}
or
while ($format !~ /^(csv|newline)$/) {
...
}
Upvotes: 7
Reputation: 12801
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.
Upvotes: 8
Reputation: 9309
while (($format ne "csv") && ($format ne "newline))
Since ||
is a short-circuiting operator, the expression ("csv" || "newline)
returns "csv" because csv is doesn't evaluate to false, so its returned first. If it was ("newline" || "csv")
, it would have returned newline (I believe).
Upvotes: 2