Ask a Question related to PERL Beginners, Design and Development.
-
Steve Grazzini #1
Re: Trivial 'unless' Question
On Tue, Oct 21, 2003 at 12:17:17PM -0700, Jeff Westman wrote:
Not really. You could use the conditional operator, though.> # ... but can I do something like....
> print "first\n" unless ($counter) else { print "second\n";
print $counter ? "second\n" : "first\n";
--
Steve
Steve Grazzini Guest
-
#9516 [Opn->Bgs]: No trivial way to bypass safe mode when running as a shell
ID: 9516 Updated by: rasmus@php.net Reported By: bram at xspace dot com -Status: Open +Status: Bogus... -
a trivial type question
When I'm doing magazine ads, there is frequently a lot of pushing and pulling to get type to fit in a certain area of the ad... for instance,... -
Trivial encryption
"William Tasso" wrote: Map everything to zero. It (a) is trivial, (b) is one-way, and (c) will confound almost anyone. -- Dave Anderson ... -
Trivial stuff
Please settle an argument... Some file extensions are pronounced as a word...like "jaypeg" for .jpg. I've been doing graphics for a few years... -
Trivial sqlplus questions
On Sat, 21 Dec 2002 20:06:35 GMT, CoolDude <nospam@nospam.it> wrote: some *sql*plus* help *can* be stored in the instance, provided one does... -
Jeff Westman #2
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
[url]http://shopping.yahoo.com[/url]
Jeff Westman Guest
-
Steve Grazzini #3
Re: Trivial 'unless' Question
On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote:
print +( qw(second first --> unless <--) )[ not $counter ];> Steve Grazzini <grazz@pobox.com> 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.
--
Steve
Steve Grazzini Guest
-
Jeff Westman #4
Re: Trivial 'unless' Question
Steve Grazzini <grazz@pobox.com> wrote:
True, but I was looking for a way to do this with a somewhat "buried" unless> 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";
keyword.
TA
-JW
__________________________________
Do you Yahoo!?
The New Yahoo! Shopping - with improved product search
[url]http://shopping.yahoo.com[/url]
Jeff Westman Guest
-
Kevin Pfeiffer #5
Re: Trivial 'unless' Question
In article <20031021195420.GE10120@grazzini.net>, Steve Grazzini wrote:
??? :-|> On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote:>>> Steve Grazzini <grazz@pobox.com> 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
Kevin Pfeiffer Guest
-
Alan Perry #6
RE: Trivial 'unless' Question
On Tuesday, October 21, 2003 16:01, Kevin Pfeiffer wrote:
I think the author was trying to be cute... I got a chuckle out of it!>In article <20031021195420.GE10120@grazzini.net>, Steve Grazzini wrote:
>>>> On Tue, Oct 21, 2003 at 01:06:44PM -0700, Jeff Westman wrote:>>>>> Steve Grazzini <grazz@pobox.com> 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 ];
>??? :-|
>
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
Alan Perry Guest
-
Roy Johnson #7
Re: Trivial 'unless' Question
[email]westfour@yahoo.com[/email] (Jeff Westman) wrote in message news:<20031021191717.66026.qmail@web41107.mail.yah oo.com>...
The predicate conditionals are limited: there is no elsing with them.> # ... but can I do something like....
> print "first\n" unless ($counter) else { print "second\n";
The suggestion to use the ternary operator (?:) is appropriate for
this example. If you really wanted to sneak some more conditional
logic into a predicate, you'll have to do it with operators:
print "first\n" unless ($counter and print "second\n");
I strongly advise against that sort of garbling. It would be much
clearer to write
print(($counter) ? 'second'
: 'first'
, "\n");
or just
if ($counter == 0) { print "first\n" }
else { print "second\n" }
Roy Johnson Guest



Reply With Quote

