Test that a string value has an integer format ?

Just starting out? Need help? Post your questions and find answers here.
Neil
Enthusiast
Enthusiast
Posts: 198
Joined: Wed Feb 29, 2012 8:04 am
Location: Melbourne, AUS

Re: Test that a string value has an integer format ???

Post by Neil »

@Luis

Sorry to bother you again but I'm still not 100% clear !!

Code: Select all

Protected *char.Character = @S$
I know this is setting the pointer to the address of the start of the string.
What is the significance of ".Character" ? Does that mean the pointer has a type ?? I didn't think they did.
Or is this to set the step increment as mentioned in your post? ("Char" in post is short for "Character").
The increment is done using the Sizeof operator instead of a simpler +1 because Char is defined to be 1 byte big in ascii and 2 bytes big in unicode

Code: Select all

*char + SizeOf(Character) 
i.e. Is the size of "Character" determined by the first code line quoted above ??

How is this loop working ?? i.e. what does the "\c" imply ??

Code: Select all

While *char\c 
Thanks again,

Neil
Morty
User
User
Posts: 35
Joined: Mon Mar 19, 2007 8:30 am
Location: Germany
Contact:

Re: Test that a string value has an integer format ???

Post by Morty »

Maybe this is a short solution ... convert it and compare it

Code: Select all

Procedure IsInteger(in_string.s)
  If Str(Val(in_string.s)) = in_string.s
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

Debug IsInteger("2")
Debug IsInteger("-2")
Debug IsInteger("2.56")
Debug IsInteger("asd")
Debug IsInteger("2300")
Debug IsInteger("-12a2")
Debug IsInteger("0")
Debug IsInteger("0.0")
Morty
Neil
Enthusiast
Enthusiast
Posts: 198
Joined: Wed Feb 29, 2012 8:04 am
Location: Melbourne, AUS

Re: Test that a string value has an integer format ???

Post by Neil »

Hi Morty,

As usual - I learn something new every time I post on the forum!!

Code: Select all

Val(in_string.s)
I just assumed (because I didn't read the "Help" properly !!) that Val() would return a "real".
Quote From Help wrote:Transforms a string into a quad numeric value.
The number parsing stops at the FIRST non numeric character.
Very neat solution.

Thanks,

Neil
buddymatkona
Enthusiast
Enthusiast
Posts: 252
Joined: Mon Aug 16, 2010 4:29 am

Re: Test that a string value has an integer format ???

Post by buddymatkona »

The Devil is in the details. :) Watch out for leading zeros with "If Str(Val(in_string.s)) = in_string.s"
Little John
Addict
Addict
Posts: 4791
Joined: Thu Jun 07, 2007 3:25 pm
Location: Berlin, Germany

Re: Test that a string value has an integer format ???

Post by Little John »

Morty, really neat solution!
buddymatkona wrote:The Devil is in the details. :) Watch out for leading zeros with "If Str(Val(in_string.s)) = in_string.s"

Code: Select all

Procedure.i IsInteger (s$)
   ProcedureReturn Bool(Str(Val(s$)) = LTrim(s$,"0"))
EndProcedure

Debug IsInteger("2")
Debug IsInteger("02")
:-)
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Test that a string value has an integer format ???

Post by luis »

Neil wrote: I know this is setting the pointer to the address of the start of the string.
What is the significance of ".Character" ? Does that mean the pointer has a type ?? I didn't think they did.
Or is this to set the step increment as mentioned in your post? ("Char" in post is short for "Character").
Character is a structure

Code: Select all

Structure Character
    c.c
EndStructure
All the "basic" types have a corresponding structure useful to be used with pointers. See Tools->Structure viewer.
Unfortunately this equivalence is not explained in the manual.

Pointers can't have a "basic" type (they are always integers) but they can be structured pointers, i.e. point to a memory area and access it through the fields of the specified structure.
The above pointer is used to access the memory buffer through the "c" field, in this case a character (".c").
Practically you access the memory one (or two if unicode) bytes at the time.
Neil wrote:

Code: Select all

*char + SizeOf(Character) 
i.e. Is the size of "Character" determined by the first code line quoted above ??
Size of Character is determined only by the the fact you are compiling to ascii or unicode. Like the size of .i (4/8 bytes) for 32 or 64 bit code generation.
Neil wrote: How is this loop working ?? i.e. what does the "\c" imply ??

Code: Select all

While *char\c 
*char is the structured pointer, defined to point to a memory area containing a character (ascii or unicode), *char\c access the field of the structure Character I showed you just above.
Incrementing the pointer using SizeOf(Character) you jump forward by 1 or 2 bytes as required and get the next character.
The loop exits when the char is a null (terminator).
"Have you tried turning it off and on again ?"
Morty
User
User
Posts: 35
Joined: Mon Mar 19, 2007 8:30 am
Location: Germany
Contact:

Re: Test that a string value has an integer format ???

Post by Morty »

Thanks
buddymatkona wrote:The Devil is in the details. :) Watch out for leading zeros with "If Str(Val(in_string.s)) = in_string.s"
Yes, that's right. My mistake in search of an easy solution ... BUT!!!
If you want to write the value to an integer database field (for example) it writes it without the leading zero. So if you have "02" -> in your database will only "2" be saved.
So the check is right -> it's not a valid integer.

It relies on the way you need the integer value check.

Morty
User avatar
Demivec
Addict
Addict
Posts: 4270
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: Test that a string value has an integer format ???

Post by Demivec »

buddymatkona wrote:The Devil is in the details. :) Watch out for leading zeros with "If Str(Val(in_string.s)) = in_string.s"
@Morty: Here's one more variation:

Code: Select all

Procedure IsInteger(s$)
   ProcedureReturn Bool(Str(Val(s$)) = LTrim(s$,"0") Or (Left(s$, 1) = "0" And LTrim(s$, "0") = ""))
EndProcedure

Debug IsInteger("236523") ; 1
Debug IsInteger("-3232")  ; 1
Debug IsInteger("0.0")    ; 0
Debug IsInteger("-10.02") ; 0
Debug IsInteger("0.0a")   ; 0
Debug IsInteger("abc")    ; 0
Debug IsInteger("0")      ; 1
Debug IsInteger("0a")     ; 0
Debug IsInteger("0000000"); 1
Neil
Enthusiast
Enthusiast
Posts: 198
Joined: Wed Feb 29, 2012 8:04 am
Location: Melbourne, AUS

Re: Test that a string value has an integer format ?

Post by Neil »

Hi All,

Thank you all very much for your input and suggestions.

As usual, I have learnt many things from fellow Forumers for what I thought would be a simple straightforward post !!

@luis
For the explanation of how pointers work. They are explained in the help file/manual, but this doesn't help you understand why you should actually use them.

@citystate
For a solution using a regular expression. I have used very basic regular expressions before (in awk programs) but I did not clearly understand how you can use them in PB.

@morty (and others following)
For a very simple and elegant solution using a PB function (and demonstrating the benefit of reading the manual "carefully" !!).

Thanks again,

Neil

BTW, this function is required in the application I am writing to process/analyze/reformat program files that I have written for the software that I use at work (PDMS - a 3D modelling package for the Oil & Gas Industry).

One of my application options is to re-indent the code (like PB IDE and jaPB do). I have a similar option window to set the "before" and "after" indents for the keywords.

Interestingly, I notice that the PB input widow does not flag an error if you enter an invalid value - it just sets the field to "0"!! I wanted my application to give a warning to the user that the input value has to be an integer.
User avatar
luis
Addict
Addict
Posts: 3895
Joined: Wed Aug 31, 2005 11:09 pm
Location: Italy

Re: Test that a string value has an integer format ?

Post by luis »

Neil wrote: For the explanation of how pointers work. They are explained in the help file/manual, but this doesn't help you understand why you should actually use them.
You are welcome, keep in mind here we just saw some of the reasons for using pointers.
Beyond the ones saw here the most common are:

To pass a variable to a procedure by reference
In PB normally you pass data by value (a copy of the original var), but if you want to modify the value of the passed variable you must use the "@" operator to actually pass its address. Then through that pointer you can access the real var (not a local copy) and change its contents.

To pass a structure to a procedure
In PB you can't pass a copy of a structure (there are some caveat, let's forget them for now) so you must pass the address of a structure.
Again you are using a pointer.

To use a callback
When you write/use a procedure which needs to communicate its progress to the main program while it's working, you can use a callback.
To call a callback your procedure needs the address of the procedure to be invoked. Again, that address it's a pointer, not to data but to a procedure.

To create dynamic structures in memory
A typical case is a linked list, or a tree. Every item of the list/tree is allocated dynamically using AllocateMemory() and each item is linked to another by storing the addresses of its neighbors as pointers.

... and much more

Just google "why use pointers" and you can read for a looooong time.
"Have you tried turning it off and on again ?"
said
Enthusiast
Enthusiast
Posts: 342
Joined: Thu Apr 14, 2011 6:07 pm

Re: Test that a string value has an integer format ?

Post by said »

my version :!: accepts exp. notation (1e+3)
Removed as it was buggy! you can look here

http://www.purebasic.fr/english/viewtop ... 12&t=68227
Post Reply