Page 1 of 1

GetTypeOf()

Posted: Fri Sep 11, 2015 5:12 pm
by falsam
With JavaScript, the TypeOf() function returns the datatype of the data contained in parameter.

For example, if I debug typeof ("test") the result is a String.

View result http://jsfiddle.net/falsam/xg3acvqs/

With PureBasic, it is not possible. It must pass a parameter with a datatype.

This short code is a workaround.

Code: Select all

; GetTypeOf(Value)
; Contributor falsam 
;
; PB 4.31 -> 4.40
;

Procedure _GetTypeOf(numstr.s)
  Protected Result, Pattern.s = "^[-+]?[0-9]*\.?[0-9]+$"
  Protected RegEx = CreateRegularExpression(#PB_Any, Pattern)
  If RegEx
    Result = MatchRegularExpression(RegEx, numstr)
    FreeRegularExpression(RegEx)
    If result
      If CountString(numstr, ".") = 1
        ProcedureReturn #PB_Float
      Else
        ProcedureReturn #PB_Integer
      EndIf
    Else
      ProcedureReturn #PB_String
    EndIf
  EndIf
EndProcedure

Macro Quote
  ""
EndMacro

Macro GetTypeOf(Value=)
  _GetTypeOf(Quote + Value + Quote)
EndMacro

Debug GetTypeOf("test") ;String
Debug GetTypeOf("10")   ;Integer
Debug GetTypeOf("10.10");Float
Debug GetTypeOf(10)     ;Integer
Debug GetTypeof(10.10)  ;Float
Debug GetTypeof(-10.10) ;Float

Re: GetTypeOf()

Posted: Sat Sep 12, 2015 11:17 am
by Lord
Nice, but...

Code: Select all

GetTypeOf(1.1)
returns 21 aka #PB_Integer?
So it tells "only" wether its a string or not?

Re: GetTypeOf()

Posted: Sat Sep 12, 2015 11:39 am
by falsam
Lord wrote:Nice,
Thank
Lord wrote:but...
Ouch! :!:
Lord wrote:So it tells "only" wether its a string or not?
Yes, the same with javascript

Re: GetTypeOf()

Posted: Sat Sep 12, 2015 11:48 am
by falsam
Update : Debug is not in the right place. Code update.

Re: GetTypeOf()

Posted: Sat Sep 12, 2015 4:11 pm
by falsam
Update code. Add : #PB_Float

Re: GetTypeOf()

Posted: Sun Sep 13, 2015 9:55 am
by Lord
falsam wrote:Update code. Add : #PB_Float
Nice, but... :wink:

...what about:
Debug 10E3
Debug GetTypeOf(10E3)