[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Groff] condition: OR of two string comparisons
From: |
Ralph Corderoy |
Subject: |
Re: [Groff] condition: OR of two string comparisons |
Date: |
Sun, 23 Nov 2014 11:32:54 +0000 |
Hi Steffen,
> I'm wondering a bit since your current interim and part-time
> favorite Python supports ask-me-the-name-for-it sequences:
>
> elif o in ('-h', '--help'):
Python's just a language I'm using for some of the examples. :-)
(42, 'foo', 3.14) is a tuple in Python. A read-only ordered
fixed-length list that can have elements of varying types. Python's
`in' operator can accept various things as its right operand, including
tuples. http://en.wikipedia.org/wiki/Tuple
Awk has an `in' operator too for its associative arrays, AKA hashes in
Perl or dicts in Python. It checks if the left operand is a key.
> . ie 'Ar'\$1' .
> . el .ie 'Cm'\$1' .
> . el .ie 'Dv'\$1' .
> . el .ie 'Er'\$1' .
> ...
> . el \{\
> . ds doc-mx-arx\n[doc-mx-ard]
> . return
> . \}
>
> And this would be terrible and expensive even with || and &&:
>
> .if $ 'Ar'\$1' || 'Cm'\$1' || 'Dv'\$1' || 'Er'\$1' || ...
No more expensive than the existing method you give above, assuming `||'
would short-circuit, as it clearly should.
> But rather easy and cheap with a sequence:
>
> .if $'\$1'Ar'Cm'Dv'Er'...$
>
> .if $ ,'\$1', 'Ar', 'Cm', 'Dv', 'Er'...
That seems to bring two advantages; '\$1' is evaluated once, perhaps
this is the expense you refer to, and the reader knows '\$1' is always
one of the operands for the compare without having to check each one.
But with a new expression syntax up for grabs, there's other
possibilities depending on what's available.
.if '\$1' == 'Ar' || '\$1' == 'Cm' || '\$1' == 'Dv' || '\$1' == 'Er' {
.if '\$1' in ('Ar', 'Cm', 'Dv', 'Er') {
.if '\$1' =~ r'^(Ar|Cm|Dv|Er)$' {
.if len('\$1') == 2 && index('\$1,', 'Ar,Cm,Dv,Er,') >= 0 {
The second would seem nicest, if such syntax existed. But even the
first would be an advantage over now.
Cheers, Ralph.