Post Reply 
Can you check for string parameters without a magic number?
11-19-2017, 08:00 AM (This post was last modified: 11-20-2017 06:43 AM by StephenG1CMZ.)
Post: #7
RE: Can you check for string parameters without a magic number?
(11-19-2017 03:05 AM)TravisE Wrote:  If I understand the OP correctly, they want to compare the types without directly using numeric constants.

(11-16-2017 07:09 AM)StephenG1CMZ Wrote:  Using type instead of TYPE even though I am in PPL not CAS doesnt seem to help

Code:

EXPORT STRINGMAGIC()
BEGIN
 PRINT();
 PRINT(IFTE(TYPE(12.5)==DOM_FLOAT-1,STRING('DOM_FLOAT'),"NOT"));
 PRINT(IFTE(TYPE(#12)==DOM_INT-1,STRING('DOM_INT'),"NOT"));
 PRINT(IFTE(TYPE("ST")==DOM_STRING-1,STRING('DOM_STRING'),"OOPS"));
 PRINT(IFTE(TYPE("ST")==3-1,"It IS a THREE, NOT A STRING","OOPS"));
 PRINT(IFTE(TYPE({})==DOM_LIST-1,STRING('DOM_LIST'),"NOT"));
 PRINT(type(12.5)==DOM_FLOAT-1);
 PRINT(type(#12)==DOM_INT-1);
 PRINT(type("ST")==DOM_STRING-1);
 PRINT(IFTE(type("ST")==DOM_STRING-1,"It IS A STRING","NO, IT IS NOT A STRING "+type("M")));
 PRINT(type({})==DOM_LIST-1);

END;
What am I missing?

From playing around a bit, it looks like it's necessary to:

1. Call the CAS type function with “CAS.type()”

2. Don't subtract 1 from the DOM_* constants you're comparing to (I'm not sure why you're doing that)

The PPL TYPE() function appears to returns a different set of values, so comparing its return values to the DOM_* constants isn't appropriate. If you really want to avoid using a magic number there, you could either define your own named constants in your program, or you could possibly use something like:

Code:

IF TYPE(SOMEVAR) == TYPE(123.4) // Check for real


IF TYPE(SOMEVAR) == TYPE("string") // Check for string

etc.

Thanks for that tip.
Using IF TYPE(...)==TYPE(1.2) etc. Seems the best way to avoid using constants like 3-1, or even DOM_STRING, which can lead you astray.
To avoid having a 1.2 here and a PI there, which would be distracting, I'd perhaps use
MY_DOM_FLOAT==TYPE(1.2)
IF TYPE(...)==MY_DOM_FLOAT

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


Messages In This Thread
RE: Can you check for string parameters without a magic number? - StephenG1CMZ - 11-19-2017 08:00 AM



User(s) browsing this thread: 1 Guest(s)