HP Forums
Can you check for string parameters without a magic number? - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Can you check for string parameters without a magic number? (/thread-9503.html)



Can you check for string parameters without a magic number? - StephenG1CMZ - 11-15-2017 09:43 PM

You can check for floats, integers, lists etc. In PPL.

Can you detect a string too? Without using a magic number...

Code:


EXPORT STRINGMAGIC()
BEGIN
 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"));


END;
yields:
DOM_FLOAT
DOM_INT
OOPS
It IS a THREE, NOT A STRING
DOM_LIST
Is there an alternative constant?


RE: Can you check for string parameters without a magic number? - Carlos295pz - 11-16-2017 12:20 AM

Exists:
type() → CAS
TYPE() → Home


RE: Can you check for string parameters without a magic number? - StephenG1CMZ - 11-16-2017 07:09 AM

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?


RE: Can you check for string parameters without a magic number? - Carlos295pz - 11-16-2017 07:28 AM

In Home → TYPE
Code:
Syntax:
TYPE(object)

Returns the type of the object:
0: Real
1: Integer
2: String
3: Complex
4: Matrix
5: Error
6: List
8: Function
9: Unit
14.?: CAS object. the fractional part is the CAS type

In CAS → type (CAS.type in Home)
Code:
Syntax:
type(Object)

Returns the type of an object.
DOM_int
DOM_FLOAT, float
DOM_INT, integer
DOM_COMPLEX, complex
DOM_IDENT 
DOM_LIST 
DOM_SYMBOLIC, symbol
DOM_RAT, rational
DOM_STRING
DOM_FUNC

The types in Home and CAS are different, they are not related, for example the format #FFh for integers in CAS does not exist


RE: Can you check for string parameters without a magic number? - Carlos295pz - 11-16-2017 07:37 AM

Uso común: TYPE(n)
Code:
EXPORT TypeString(n)
BEGIN
  n:=TYPE(n);
  CASE
    IF n=0 THEN "Real" END
    IF n=1 THEN "Integer" END
    IF n=2 THEN "String" END
    IF n=3 THEN "Complex" END
    IF n=4 THEN "Matrix" END
    IF n=6 THEN "List" END
    IF n=8 THEN "Function" END
    IF n=9 THEN "Unit" END
     "?"
  END
END;



RE: Can you check for string parameters without a magic number? - TravisE - 11-19-2017 03:05 AM

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.


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

(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


RE: Can you check for string parameters without a magic number? - Carlos295pz - 11-20-2017 04:38 AM

* In Home

[Image: 38502735732_661645192a_o.png]

Code:
IF TYPE(VAR)=0 THEN ... //REAL
IF TYPE(VAR)=1 THEN ... //INTEGER
IF TYPE(VAR)=2 THEN ... //STRING
IF TYPE(VAR)=6 THEN ... //LIST


* In Home with type (CAS)

[Image: 38534358291_0d5831448e_o.png]

[Image: 38502735592_87940e5c55_o.png]

Code:
IF CAS.type(VAR)=1 THEN ... //REAL
IF CAS.type(VAR)=DOM_FLOAT THEN ... //REAL

IF CAS.type(VAR)=12 THEN ... //STRING
IF CAS.type(VAR)=DOM_STRING THEN ... //STRING

IF CAS.type(VAR)=7 THEN ... //LIST
IF CAS.type(VAR)=DOM_LIST THEN ... //LIST