Trivial 'unless' Question
Hi . very trivial ... is there a way or correct syntax to add an 'if' tp the following 'unless' statement? # this works fine ... print first\n unless ($counter); # ... but can I do something like print first\n unless ($counter) else { print second\n; # (syntax error) I know I can do this: #!/usr/bin/perl use warnings; use strict; my $counter = 0; unless ($counter) { print first\n; } else { print second\n; } but I wanted to put the 'unless' later in the statement. I couldn't find anything in the FAQ or perldoc on this one. TIA! -JW __ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Trivial 'unless' Question
On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote: # ... but can I do something like print first\n unless ($counter) else { print second\n; Not really. You could use the conditional operator, though. print $counter ? second\n : first\n; -- Steve -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Trivial 'unless' Question
Steve Grazzini [EMAIL PROTECTED] wrote: On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote: # ... but can I do something like print first\n unless ($counter) else { print second\n; Not really. You could use the conditional operator, though. print $counter ? second\n : first\n; True, but I was looking for a way to do this with a somewhat buried unless keyword. TA -JW __ Do you Yahoo!? The New Yahoo! Shopping - with improved product search http://shopping.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Trivial 'unless' Question
On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote: Steve Grazzini [EMAIL PROTECTED] wrote: On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote: # ... but can I do something like print first\n unless ($counter) else { print second\n; Not really. You could use the conditional operator, though. print $counter ? second\n : first\n; True, but I was looking for a way to do this with a somewhat buried unless keyword. print +( qw(second first -- unless --) )[ not $counter ]; -- Steve -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
Re: Trivial 'unless' Question
In article [EMAIL PROTECTED], Steve Grazzini wrote: On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote: Steve Grazzini [EMAIL PROTECTED] wrote: On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote: # ... but can I do something like print first\n unless ($counter) else { print second\n; Not really. You could use the conditional operator, though. print $counter ? second\n : first\n; True, but I was looking for a way to do this with a somewhat buried unless keyword. print +( qw(second first -- unless --) )[ not $counter ]; ??? :-| -- Kevin Pfeiffer -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
RE: Trivial 'unless' Question
On Tuesday, October 21, 2003 16:01, Kevin Pfeiffer wrote: In article [EMAIL PROTECTED], Steve Grazzini wrote: On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote: Steve Grazzini [EMAIL PROTECTED] wrote: On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote: # ... but can I do something like print first\n unless ($counter) else { print second\n; Not really. You could use the conditional operator, though. print $counter ? second\n : first\n; True, but I was looking for a way to do this with a somewhat buried unless keyword. print +( qw(second first -- unless --) )[ not $counter ]; ??? :-| I think the author was trying to be cute... I got a chuckle out of it! print +( qw(second first -- unless --) )[ not $counter ]; Basically, you are creating an anonymous (right term?) array, and printing the not $counter'th element. So if $counter is 0, you print first, otherwise you print second. The other stuff in the array is there just to satisfy the buried unless requested by the OP. However, as you see here, it does not use the keyword unless, just a string unless. The only elements that will ever be accessed are second and first... It could also be written as: print +( qw(second first) )[ not $counter ]; HTH Alan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]