Newsgroups: | comp.lang.perl |
Path: | icdoc!dds |
From: | dds@doc.ic.ac.uk (Diomidis Spinellis) |
Subject: | Re: (Silly?) idea for new perl feature |
Nntp-Posting-Host: | skid.doc.ic.ac.uk |
Message-ID: | <1992Jan11.171508.8166@doc.ic.ac.uk> |
Summary: | already exists |
Organization: | Dept. of Computing, Imperial College, London, England |
Keywords: | ternary compare evaluate |
Date: | Sat, 11 Jan 1992 17:15:08 GMT |
References: | <1992Jan10.203455.22219@abekrd.co.uk> |
Lines: | 23 |
Content-Length: | 875 |
In article <1992Jan10.203455.22219@abekrd.co.uk> garyb@abekrd.co.uk (Gary Bartlett) writes:
[...]
>Now, perl has added the operators <=> and cmp which produce values
>with three outcomes, (stored internally as -1, 0 and 1). Wouldn't it
>be nice to have a perl operator similar to '? :' which acted on
>these three outcome values?
>
>eg - $ans = (x <=> 2) ?? 10 :: 9 :: 8
>
>ie - if 'x' is less than 2, then set 'ans' to 10; if 'x' is equal to
> 2, then set 'ans' to 9; if 'x' is greater than 2, then set 'ans'
> to 8.
[...]
Perl already supports such a construct:
$ans = (10, 9, 8)[($x <=> 2) + 1];
Diomidis
--
Diomidis Spinellis Internet: dds@doc.ic.ac.uk
Department of Computing UUCP: ...!ukc!icdoc!dds
Imperial College, London SW7 #include "/dev/tty"
Newsgroup comp.lang.perl contents
Newsgroup list
Diomidis Spinellis home page
Unless otherwise expressly stated, all original material on this page created by Diomidis Spinellis is licensed under a Creative Commons Attribution-Share Alike 3.0 Greece License.