Page 2 of 4

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 7:45 am
by sancho2
mao wrote:Thanks all!
I am just curious why it only takes 19 short-line code(two DO-LOOP and one FOR-NEXT loop) in QuickBasic to parse this file, but in PureBasic we have to write a much longer one. PureBasic is suposed to be more powerful than QB,right? :?
Very short QB code. Why not post it and get much better results?

edit:
mao wrote: The second part of the file is filled with numeric values with specific length and separated by space and every following line is shorter than by one value. I need to put these values into a matrix.
When mao says "matrix" could it be that he wants 2 dimension array?

Code: Select all

myArray()
  1     2     3     4
1 1.098 2.754 3.777 5.777
2 1.999 3.777 4.567
3 2.888 4.675
4 1.897
I think we could use more info.

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 8:30 am
by Marc56us
Hi :P

Code: Select all

; Read Log

; Simple, quick and dirty version for learning 
; Tutorial for user coming from other Basic
; Do this at home but dont do this at work ;-)
; Explain how to use StringField to split text line
; and Trim to remove unwanted char
; Array are Zero base, change if you want

; Classic array version (you can use small array too and use ReDim to expand)
; Array in PB use () instead of []
Dim Data_One$(100, 2)
Dim Data_Two$(100, 10)	; I use matrix 2 as string for easy fixed decimal (use Val() or ValF() after)

; ReadFile instead of OpenFile can open file (readonly) event if shared (windows only)
If ReadFile(0, "datas.log")
	
	While Not Eof(0)
		
		TmpLine$ = ReadString(0)
		id		 = Asc( Left(TmpLine$, 1) )		; ASCII value of first char of line	
		i + 1									; increment i (can be write as i = i + 1)
		
		Select id
				
			Case 34		
				; Double-quote 	
				Debug "Line " + Str(i) + " is Part 1      : " + TmpLine$
				; Trim (string, chr(34) while remove "" at beging and end of string
				Data_One$(D1, 0) = Trim( StringField(TmpLine$, 1, ","), Chr(34) )
				Data_One$(D1, 1) = Trim( StringField(TmpLine$, 2, ","), Chr(34) )
				D1 + 1
				
			Case 46  	
				; Dot
				Debug "Line " + Str(i) + " is a Separator : " + TmpLine$
				; Nothing to do here
				
			Case 48 To 57
				; number 0 to 9
				Debug "Line " + Str(i) + " is Part 2      : " + TmpLine$
				RTrim(TmpLine$)  ; remove spaces at end of line
				k = 1
				For j = 0 To CountString(TmpLine$, " ")
					Data_Two$(D2, j) = StringField(TmpLine$, k, " ")
					k + 1
				Next
				D2 + 1
				
		EndSelect
		
	Wend
	CloseFile(0)
	
Else 
	Debug "Uh? I can't find datafile :-/"
	End
EndIf

Debug "--------- Results ---------"
Debug "--- Matrix 1"
For i = 0 To D1 - 1
	Debug " "  + Data_One$(i, 0) + " - " + Data_One$(i, 1)	
Next i
Debug "--- Matrix 2"
For i = 0 To D2 - 1
	Debug " " + Data_Two$(i, 0) + " - " + Data_Two$(i, 1) + " - " + Data_Two$(i, 2) + " - " + Data_Two$(i, 3)
Next

End
REM: Here, this code is unreadble, to read clearly
- Copy / Paste this code into PB
- Select all (CTRL+A)
- Auto indent (CTRL + I)

Hope this help :wink:

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 9:12 am
by TI-994A
mao wrote:...it only takes 19 short-line code(two DO-LOOP and one FOR-NEXT loop) in QuickBasic to parse this file, but in PureBasic we have to write a much longer one.
These read and write routines are extracted from this earlier post. Only ten lines each:

routine to write the file:

Code: Select all

OpenFile(1, fileName)
For r = 1 To 3
  For i = 0 To 3
    WriteStringN(1, textArray(i))
  Next i
  For i = 0 To 9
    WriteDouble(1, doublesArray(i))
  Next i
Next r
CloseFile(1)
routine to read the above-written file:

Code: Select all

OpenFile(1, fileName)
For r = 1 To 3
  For i = 1 To 4
    Debug ReadString(1)
  Next i
  For i = 1 To 10
    Debug StrD(ReadDouble(1))
  Next i
Next r
CloseFile(1)
mao wrote:PureBasic is suposed to be more powerful than QB,right? :?
It's not supposed to be...IT IS! :wink:

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 1:43 pm
by mao
"Haemophilus"," Oral Taxon 535"
"Aggregatibacter"," sp. Oral Taxon 513 clone"
"Haemophilus"," sp. Oral Taxon 036 clone "
"Haemophilus"," sp. Oral Taxon 035 clone "
"Aggregatibacter"," Oral Taxon 720 "
"Haemophilus haemolyticus","Oral Taxon 851 "
"Haemophilus ducreyi"," Oral Taxon 821 "
"Aggregatibacter"," sp. Oral Taxon 898 clone "
"Aggregatibacter"," sp. Oral Taxon 512 clone "
"Haemophilus sp.","Oral Taxon 908 clone"
"Aggregatibacter aphrophilus"," Oral Taxon 545 "
"Aggregatibacter segnis"," Oral Taxon 762"
"Aggregatibacter"," sp. Oral Taxon 458 clone "

4.25 3.78 5.90 5.44 3.29 5.65 5.99 4.15 3.27 5.14 4.92 4.78
3.43 5.17 2.18 4.73 6.83 2.11 1.29 3.27 2.84 0.97 1.13
3.01 3.01 2.67 7.35 4.17 2.76 1.83 3.70 3.23 2.02
4.32 4.52 3.70 5.17 4.42 3.83 4.32 4.72 4.53
4.24 6.17 2.43 1.69 3.38 0.97 1.69 2.18
7.65 3.32 3.02 1.47 4.23 4.73 3.35
6.12 5.19 3.61 7.32 6.05 6.02
2.32 3.18 2.06 2.18 2.55
3.95 2.01 7.50 0.70
6.02 3.27 2.80
2.40 2.93
0.21




Hi All! Here is the original file(slightly changed). Yes, the first(text) part I want to store into two arrays and the second part I plan to put them into a matrix(2 dimension array). Sorry about the confuse. Thanks for all your codes and suggestions!

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 1:44 pm
by mao
"Haemophilus"," Oral Taxon 535"
"Aggregatibacter"," sp. Oral Taxon 513 clone"
"Haemophilus"," sp. Oral Taxon 036 clone "
"Haemophilus"," sp. Oral Taxon 035 clone "
"Aggregatibacter"," Oral Taxon 720 "
"Haemophilus haemolyticus","Oral Taxon 851 "
"Haemophilus ducreyi"," Oral Taxon 821 "
"Aggregatibacter"," sp. Oral Taxon 898 clone "
"Aggregatibacter"," sp. Oral Taxon 512 clone "
"Haemophilus sp.","Oral Taxon 908 clone"
"Aggregatibacter aphrophilus"," Oral Taxon 545 "
"Aggregatibacter segnis"," Oral Taxon 762"
"Aggregatibacter"," sp. Oral Taxon 458 clone "

4.25 3.78 5.90 5.44 3.29 5.65 5.99 4.15 3.27 5.14 4.92 4.78
3.43 5.17 2.18 4.73 6.83 2.11 1.29 3.27 2.84 0.97 1.13
3.01 3.01 2.67 7.35 4.17 2.76 1.83 3.70 3.23 2.02
4.32 4.52 3.70 5.17 4.42 3.83 4.32 4.72 4.53
4.24 6.17 2.43 1.69 3.38 0.97 1.69 2.18
7.65 3.32 3.02 1.47 4.23 4.73 3.35
6.12 5.19 3.61 7.32 6.05 6.02
2.32 3.18 2.06 2.18 2.55
3.95 2.01 7.50 0.70
6.02 3.27 2.80
2.40 2.93
0.21


sorry about the previous green color...

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 1:50 pm
by mao
original PB code

n = 76

DIM dm(n, n)
DIM array1$(n)
DIM array2$(n)

OPEN "..\example.txt" FOR INPUT AS #1
DO
INPUT #1, z$
LOOP UNTIL z$ = ""
n = 0
DO
n = n + 1
INPUT #1, array1$(n)
IF array1$(n) = "" THEN EXIT DO
INPUT #1, array2$(n)
LOOP
notu = n - 1
FOR i = 1 TO notu - 1
FOR j = i + 1 TO notu
INPUT #1, dm(i, j)
NEXT
NEXT
CLOSE #1

###the 2-dimension array is not complete; some fields are empty at this stage.

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 3:05 pm
by Marc56us
I'm not about to have fully understood the statement, but here's my proposal.

Code: Select all

; Read Log 2.0

; Simple, quick and dirty version for learning 
; Tutorial for user coming from other Basic
; Do this at home but dont do this at work ;-)
; Explain how to use StringField to split text line
; and Trim to remove unwanted char
; Array are Zero base, change if you want

; Modified with new informations
n = 76

Dim dm$(n,n) 	; To use as fixed decimal, I store values as text. If you want decimal, use dm.f(n,n)	
Dim array1$(n)
Dim array2$(n)

; ReadFile instead of OpenFile can open file (readonly) event if shared (windows only)
If ReadFile(0, "example.txt")
	
	While Not Eof(0)
		
		TmpLine$ = ReadString(0)
		id		 = Asc( Left(TmpLine$, 1) )		; ASCII value of first char of line	
		i + 1									; increment i (can be write as i = i + 1)
		
		Select id
				
			Case 34		
				; Double-quote 	
				Debug "Line " + Str(i) + " Part 1 : " + TmpLine$			
				array1$(D1) = Trim( StringField(TmpLine$, 1, ","), Chr(34) )
				array2$(D1) = Trim( StringField(TmpLine$, 2, ","), Chr(34) )
				D1 + 1
				
			Case 48 To 57
				; number 0 to 9
				Debug "Line " + Str(i) + " Part 2 : " + TmpLine$
				RTrim(TmpLine$)  ; remove spaces at end of line
				k = 1
				For j = 0 To CountString(TmpLine$, " ")
					dm$(D2, j) = StringField(TmpLine$, k, " ")
					; decimal version below (need conversion ValF ou ValD)
					; dm(D2, j) = ValF(StringField(TmpLine$, k, " "))
					k + 1
				Next
				D2 + 1
				
		EndSelect
		
	Wend
	CloseFile(0)
	
Else 
	Debug "Uh? I can't find datafile :-/"
	End
EndIf

Debug "--------- Results ---------"
Debug "--------- Matrix 1"
For i = 0 To D1 - 1
	Debug "array1$(" + RSet(Str(i), 2) + ") = "  + LSet(array1$(i), 30) + " - array2$(" + RSet(Str(i), 2) + ") = " + array2$(i)	
Next i

Debug "--------- Matrix 2"
For i = 0 To D2 - 1
	Debug "--- dm(" + Str(i) + ") ---"
	For j = 0 To 11
		Debug " dm$(" + RSet(Str(i), 2) + "," + RSet(Str(j), 2) + ") : " + dm$(i, j)
		; Debug " dm(" + RSet(Str(i), 2) + "," + RSet(Str(j), 2) + ") : " + StrF(dm(i, j))
	Next j
Next i

End
(horrible) Debug output at the end is for testing only, don't worry :mrgreen:
You can use Debugger / VariableViewer instead.

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 3:44 pm
by IdeasVacuum
original PB code
Hi mao
I think that's the original Quick Basic code........ :D

I see no point in trying to mimic the QB code in PB, even though it clearly can be done.

I note that my example code parses your 2nd data sample well (it does not expect an empty line!), which itself is an argument for doing it the "PB way", though you might not be displaying the values on a Window in your real app.

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 4:40 pm
by mao
IdeasVacuum wrote:
original PB code
Hi mao
I think that's the original Quick Basic code........ :D

I see no point in trying to mimic the QB code in PB, even though it clearly can be done.

I note that my example code parses your 2nd data sample well (it does not expect an empty line!), which itself is an argument for doing it the "PB way", though you might not be displaying the values on a Window in your real app.
Yes, it is Quick Basic. Sorry for the typo... :oops:
I wrote a program that returns the output I want but it is terrible; I may post it later to get some suggestions from here....

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 4:52 pm
by mao

Code: Select all

OpenFile(1,"example.TXT")
n = 0
While Eof(1) = 0
  String.s = ReadString(1)
  If FindString(String, ",")
    n = n + 1
     
  EndIf 
    
Wend
CloseFile(1)

Dim otu.s(n-1)
Dim otu2.s(n-1)
OpenFile(1,"example.TXT")
n = 0
While Eof(1) = 0
  String.s = ReadString(1)
  If FindString(String, ",")
    
    cp = FindString(String, ",")
    
    otu(n) = Trim(Left(String,cp-1),Chr(34))    ; store the part before comma into otu
    otu2(n) = Trim(Right(String,Len(String)-cp), Chr(34))  ; store the part after the comma into otu2
    n = n + 1
  EndIf 
    
Wend
CloseFile(1)


For k=0 To n-1
  Debug otu(k) + " " + otu2(k)
Next


Dim dm.f(n,n)

OpenFile(1,"example.TXT")
i = 1
While Eof(1) = 0
  String.s = Trim(ReadString(1))
  If FindString(String, ",") = 0 And String <> ""   
    num = CountString(String, "  ")
    For k= 1 To num+1
      dm(i,i+k) = ValF(StringField(String, k, "  "))
    Next
    i+1
    
  EndIf 
    
Wend
CloseFile(1)
For i =1 To n
  For j = 1 To n
    Debug dm(i,j)
    
  Next
  
Next


;first program using PB
;welcome any suggestions
;Thanks

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 5:59 pm
by Tenaja
mao, please wrap code snips within the code brackets. There is a button just above the forum message editor to simplify it.

Re: How does PureBasic parse files?

Posted: Thu May 28, 2015 6:53 pm
by mao
Tenaja wrote:mao, please wrap code snips within the code brackets. There is a button just above the forum message editor to simplify it.
Didn't know that.... :oops:
Thanks

Re: How does PureBasic parse files?

Posted: Fri May 29, 2015 1:29 am
by IdeasVacuum
Hi mao

Looks like we are going to struggle to pull you out of Quick Basic mode :mrgreen:

For example, why hard-code a file path? Why read the file multiple times? I think perhaps the answer is - what your app does next. What we need then is the whole picture, we need to know what the goal of your app is so that the snippets we post for you are appropriate. At the moment, practically everything everyone has shown you is not being used......

Re: How does PureBasic parse files?

Posted: Fri May 29, 2015 2:32 am
by mao
IdeasVacuum wrote:Hi mao

Looks like we are going to struggle to pull you out of Quick Basic mode :mrgreen:

For example, why hard-code a file path? Why read the file multiple times? I think perhaps the answer is - what your app does next. What we need then is the whole picture, we need to know what the goal of your app is so that the snippets we post for you are appropriate. At the moment, practically everything everyone has shown you is not being used......
I really appreciate all your help; I am reading and trying to understand all the codes posted here.
All the codes showed here are far more better than mine, and I wish I could write a better one too. However, this is the first time that I have ever touched BASIC language, not only PureBasic but also QuickBasic. I think I need to get the output first and then polish the code. I'd like to learn new languages, but it does take some time. Again, really appreciate all the help and suggestion.

Re: How does PureBasic parse files?

Posted: Fri May 29, 2015 2:58 am
by mao
Zebuddi123 wrote:Hi mao This parses the file as you have described and prints the results to the debug window. Its based on parsing the file till the separator is reached, whereby the sep variable is set to 1 and now all subsequent reads are placed in the numbers linkedlist of which is of type string so can be passed to the procedure SortData(). Just an example for you to look at

Zebuddi. :)

Code: Select all

; save this data in in file w.txt in same directory as the code, without the ;"

; "abcd","abcdsssss"
; "aaaaaaaabbs","ffffffffffffffff"
; .......
; 1.098 2.754 3.777 5.777
; 1.999 3.777 4.567
; 2.888 4.675
; 1.897




Global NewList string.s()
Global NewList numbers.s() 


Procedure SortData(List l.s(), delim$) ; takes a linkedlist of type .s  and delimiter for the stringfield() serperator
	commas=CountString(l.s(),delim$) ; counts the number of comma`s in the string
	If commas 
		For i=1 To commas+1 ; +1 parses to the end of the string
			Debug StringField(l.s(), i, delim$) 
		Next
	Else
		Debug l.s() ; only one element in the string
	EndIf
EndProcedure		


If ReadFile(0,"w.txt")
	sep=0 ; to show separator not yet reached
	While Not Eof(0)
		a$=ReadString(0,ReadStringFormat(0))  ; read in the current line in the correct format ascii/utf8 etc
		If FindString(a$, Chr(34)) ; looking for chr(44) which is quotemark "
			AddElement(string()) : string()=a$  ; add an element to the linkedlist and stores the string (strings identified by quotemarks" ")
		Else 
			[color=#0000FF]If sep = 0 ; we have encountered the seperator
				a$=ReadString(0,ReadStringFormat(0)) ; ditch the seperator line and read the next line
				sep+1 ; set var that we have pasted the seperator line 
			EndIf	[/color]
			AddElement(numbers())  : numbers()=a$ ; add an element to the linkedlist and stores the string (numbers)
		EndIf	
	Wend
	
	CloseFile(0)

	
	ForEach string()
		SortData(string(), ",") ; loop through linkedlist and process the data
	Next
	
	ForEach numbers()
		SortData(numbers(), " ") ; loop through linkedlist and process the data
	Next
	
EndIf
like the SortData procedure; but a little confused about the colored IF-ENDIF, can I just use the AddElement(numbers()) : numbers()=a$ ; ?