Left-bracket problem in a string

Everything else that doesn't fall into one of the other PB categories.
^OO^
User
User
Posts: 27
Joined: Mon Apr 28, 2003 5:58 am
Location: United Kingdom
Contact:

Left-bracket problem in a string

Post by ^OO^ »

Hi y'all :D

I have come across a wierd problem in string disection.
I wrote this bit of code to illustrate it ...
Please notice that math_b has a pair of brackets.
See what this does to the output!

Code: Select all

; Disect a string into its constituent parts for parsing ...
Dim s.s(64) ; hold the parts in this array
Global c.s
Global math_a.s
Global math_b.s

; setup example srings ...
math_a="14.582*3.2/4.5-625+10.1*17.2/13.4-25.123"
math_b="14.582*3.2/4.5-(625)+10.1*17.2/13.4-25.123"

Procedure Test(M.s)
  For i=0 To 63
    s(i)=""
  Next
  n=0
  For i=1 To Len(M)
    c=Mid(M,i,1)
    If c="*" Or c="(" Or c="/" Or c=")" Or c="+" Or c="-"
      n+1
      s(n)=c
      n+1
    Else
      s(n)=s(n)+c
    EndIf
  Next
  WriteString(Chr(10)+Chr(10)+M+Chr(10)+Chr(10))
  n=0
  While s(n)>""
    WriteString(":"+s(n)+","+Chr(10))
    n+1
  Wend
EndProcedure

; make an output file for the results ...
r=CreateFile(1,"output.txt")
Test(math_a)
Test(math_b)
CloseFile(1)
End
It is holding up my efforts to produce a math expression parser. :cry:
Is this a bug?
Is it deliberate for some purpose?
Can anyone see a way round it?
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

You create a hole in the array(i.e. an empty string) whenever you encounter two reserved characters in a row, this makes your save routine fail...
^OO^
User
User
Posts: 27
Joined: Mon Apr 28, 2003 5:58 am
Location: United Kingdom
Contact:

Post by ^OO^ »

Hi Pupil,
this makes your save routine fail...
This problem is not confined to saving.
Try windowing it in a string gadget.
I wrote the little demo to show what happens when I try to separate a string into characters for analysis.
Can you suggest a way of storing and manipulating strings that contain brackets?
User avatar
Paul
PureBasic Expert
PureBasic Expert
Posts: 1282
Joined: Fri Apr 25, 2003 4:34 pm
Location: Canada
Contact:

Post by Paul »

I think that your logic is a bit wrong.
Try changing this part of your procedure:

Code: Select all

  n=0 
  While s(n)>"" 
    WriteString(":"+s(n)+","+Chr(10)) 
    n+1 
  Wend 
to this:

Code: Select all

  For tmp=0 To n 
    WriteStringN(":"+s(tmp)+","+Chr(10))  
  Next
Maybe now you see your problem? ;)
^OO^
User
User
Posts: 27
Joined: Mon Apr 28, 2003 5:58 am
Location: United Kingdom
Contact:

Post by ^OO^ »

Hi Paul, :)

I am pretending that the two math strings are unpredictable user input.
The reason I did it the way I did it was that the strings break into irregular sized chunks.
This means that the number of instances of s() cannot be known in advance.
For instance, "14.582" is one chunk. it is six chars long. "*" is another chunk, a smaller one.

8) None of this is touching on the point ...
This whole thing as about the peculiar problem presented by "(" and ")".
Why don't you mess around with this code and make math_b print out as neatly as math_a?
If you find, as I have, that this is not possible, perhaps you can pin down the reason? :wink:

Should you succeed in either of these outcomes, I shall be intensely interested as,
so far, the solution has eluded me. :(
Pupil
Enthusiast
Enthusiast
Posts: 715
Joined: Fri Apr 25, 2003 3:56 pm

Post by Pupil »

I told you what the problem was, just change your save routine to this and say otherwise :)

Code: Select all

  While n<64
    If s(n) <> ""
      WriteString(":"+s(n)+","+Chr(10))
    EndIf
    n+1
  Wend
Before you had this:

Code: Select all

    While s(n) > ""
       ...
    Wend
what do you think happend when an empty string is in s(n)? -The loop will stop.
Why is there an empy string in the middle of the array? -It's because when two control characters('+-*/()') is encountered in a row your routine jumps one instance extra (n+1) without storing something in the array and thus effecively creating a hole in the array...
^OO^
User
User
Posts: 27
Joined: Mon Apr 28, 2003 5:58 am
Location: United Kingdom
Contact:

Post by ^OO^ »

Hi, Pupil

I wrote it the way I wrote it to demonstrate the problem. Having the output terminate prematurely shows the problem and where it occours.

I NEED to store these charaters, individally, in the array, not leaving any blanks. Not just to print them. I need the array to be intact so that I can do the math expression parsing. This 'control character' behavior is a major obstruction.

Oooh. I just had a thought. Must do an experiment. I'll tell you what happens ===> :wink:

Ah ha! It worked - a little for-next loop to remove the blanks.
:D Onwards!
Post Reply