GetTypeOf()

Share your advanced PureBasic knowledge/code with the community.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

GetTypeOf()

Post 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
Last edited by falsam on Sat Sep 12, 2015 10:43 pm, edited 3 times in total.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: GetTypeOf()

Post 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?
Image
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: GetTypeOf()

Post 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

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: GetTypeOf()

Post by falsam »

Update : Debug is not in the right place. Code update.

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
falsam
Enthusiast
Enthusiast
Posts: 630
Joined: Wed Sep 21, 2011 9:11 am
Location: France
Contact:

Re: GetTypeOf()

Post by falsam »

Update code. Add : #PB_Float

➽ Windows 11 64-bit - PB 6.0 x64 - AMD Ryzen 7 - NVIDIA GeForce GTX 1650 Ti

Sorry for my bad english and the Dunning–Kruger effect.
User avatar
Lord
Addict
Addict
Posts: 849
Joined: Tue May 26, 2009 2:11 pm

Re: GetTypeOf()

Post by Lord »

falsam wrote:Update code. Add : #PB_Float
Nice, but... :wink:

...what about:
Debug 10E3
Debug GetTypeOf(10E3)
Image
Post Reply