Article View: bit.listserv.script-l
Article #401Re: How to test a variable
From: Mark C. Lawrence
Date: Wed, 02 Mar 1994 18:48
Date: Wed, 02 Mar 1994 18:48
105 lines
4311 bytes
4311 bytes
REPLY TO 03/01/94 21:33 FROM SCRIPT-L@UGA.BITNET "IBM vs Waterloo SCRIPT
discussion group": How to test a variable
Benjamin Chi <BEC@ALBANY.EDU> writes,
>I have a situation where if variable x contains the string "XXX" I want
>one thing to happen; if it contains anything else (say, "YYY ZZZ") I want
>something else to happen. How do I perform this test?
>
>.se x = 'XXX'
>.if &x = XXX;.th FOO;.el BAR
>.if XXX = &x;.th FOO;.el BAR
>
>gives FOO BAR when processed by SCRIPT; I should think it would give
>FOO FOO . If I replace the first statement with
>
>.se x = 'YYY ZZZ'
>
>then I get the diagnostic statement
>INVALID KEYWORD FOR CONDITION ON .IF.
>'TRY' LINE 3: .if YYY ZZZ = XXX;.th FOO;.el BAR
>(which I understand), and still FOO BAR .
>
>Questions:
>(1) Why aren't the two .if statements in either of the two examples
>entirely equivalent?
>(1a) Why didn't I get the same diagnostic for line 2 of the second
>example?
>(2) What is the syntactically correct way to test the contents of a
>variable whose value may be a string with embedded spaces?
There are two problems. First, if the string to be compared may include
blanks, then you need to enclose it in delimiters (such as quotes) on the .IF
statement. Second, &x is the name of a built-in function (hex to string) so
can be ambiguous. I did some tests with "input trace" turned on:
.se x = 'XXX'
.if &x = XXX;.th FOO;.el BAR
.if XXX = &x;.th FOO;.el BAR
.it all on
.se x = 'YYY ZZZ'
.if '&x' = 'XXX';.th FOO;.el BAR
.sk 2
.if 'XXX' = '&x';.th FOO;.el BAR
==============output================================
FOO FOO <= as expected
BAR <= also as expected
th FOO;.el BAR <= after error, following
stuff treated as text
==============trace=================================
00000008 .if '&x' = 'XXX';.th FOO;.el BAR
*S* .if 'YYY ZZZ' = 'XXX';.th FOO;.el BAR
00000010 .if 'XXX' = '&x';.th FOO;.el BAR
*S* .if 'XXX' = ';th FOO;.el BAR
*T* .if 'XXX' = '
SCRW015E MISMATCHED DELIMITER ON STRING.
ERROR OCCURRED AT LINE 00000010 OF FILE SYSIN:
But the following works. I added a period after "x" to delimit the symbol.
.if 'XXX' = '
*
.se x = 'XXX'
.if &x = XXX;.th FOO;.el BAR
.if XXX = &x;.th FOO;.el BAR
.it all on
.se x = 'YYY ZZZ'
.if '&x' = 'XXX';.th FOO;.el BAR
.sk 2
.if 'XXX' = '&x.';.th FOO;.el BAR
==============output====================================
FOO FOO
BAR
BAR
==============trace=================================
00000008 .if '&x' = 'XXX';.th FOO;.el BAR
*S* .if 'YYY ZZZ' = 'XXX';.th FOO;.el BAR
00000010 .if 'XXX' = '&x.';.th FOO;.el BAR
*S* .if 'XXX' = 'YYY ZZZ';.th FOO;.el BAR
If you use a symbol name that can't be mistaken for the name of a built-in
function, this also fixes it:
.se mm = 'YYY ZZZ'
.if '&mm' = 'XXX';.th FOO;.el BAR
.if 'XXX' = '&mm';.th FOO;.el BAR
==============output====================================
BAR BAR
========================================================
Mark C. Lawrence
Systems Programmer Internet: M.Lawrence@Forsythe.Stanford.edu
Stanford Data Center Bitnet: M.Lawrence@STANFORD
Stanford, CA 94305-4136 Tel: (415) 723-4976
To: SCRIPT-L@UGA.BITNET
Technical Details
Message-ID:
<SCRIPT-L%94030213490600@UGA.CC.UGA.EDU>Path:
arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!paladin.american.edu!auvm!STANFORD.BITNET!M.LawrenceShow Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 11353d,2be3885144b57674 X-Google-Attributes: gid11353d,public X-Google-ArrivalTime: 1994-03-02 10:48:03 PST Path: arc1-ca.pugleaf.net!gmd.de!newsserver.jvnc.net!howland.reston.ans.net!paladin.american.edu!auvm!STANFORD.BITNET!M.Lawrence Comments: Gated by NETNEWS@AUVM.AMERICAN.EDU Newsgroups: bit.listserv.script-l Return-Path: <@AUVM.AMERICAN.EDU,@UGA.CC.UGA.EDU:owner-script-l@UGA.CC.UGA.EDU> Return-Path: <@UGA.CC.UGA.EDU:M.Lawrence@STANFORD.BITNET> Message-ID: <SCRIPT-L%94030213490600@UGA.CC.UGA.EDU> Date: Wed, 2 Mar 1994 10:48:03 PST Sender: IBM vs Waterloo SCRIPT discussion group <SCRIPT-L@UGA.BITNET> From: "Mark C. Lawrence" <M.Lawrence@STANFORD.BITNET> Subject: Re: How to test a variable Lines: 104