Please help me to figure out the problem

Just starting out? Need help? Post your questions and find answers here.
Yuri
New User
New User
Posts: 8
Joined: Tue Aug 26, 2003 10:13 am
Location: Italy
Contact:

Please help me to figure out the problem

Post by Yuri »

hi to all, i've written this program to sort a file alphabetically but when i execute , it remove two lines from the text file.. i tried to process the same file until it goes empty!!! but i can't figure out the problem!!!
Someone may help me?

here's the program:

Code: Select all

Dim array$(10000)
filename$ = OpenFileRequester("Seleziona un file da ordinare in ordine ALFABETICO", "*.csv", "Comma Separated Value", 0)
If filename$="" Or filename$=".csv"
 MessageRequester("Errore", "Nessun file selezionato", #PB_MessageRequester_Ok) 
 End
EndIf
r = OpenFile(1, filename$)
If r=0
 MessageRequester("Errore", "Nome file invalido", #PB_MessageRequester_Ok) 
 End
EndIf
pos=1
eof=0
Repeat
text$ = ReadString()
If text$=""
 eof=1
 Goto fondo
EndIf
array$(pos) = text$
pos=pos+1
fondo:
Until eof=1
CloseFile(1)
quantity=pos-1
SortArray(array$(),2)
DeleteFile(filename$)
OpenFile(1,filename$)
pos=1
Repeat
If array$(pos)<>""
 WriteStringN(array$(pos))
EndIf
pos=pos+1
Until pos=9999
CloseFile(1)
MessageRequester("Finito", "Ordinati "+Str(quantity)+" elementi", #PB_MessageRequester_Ok) 
and here's a sample CSV file:

Code: Select all

Fifth Gear,Racing game,5thgear,1988,Rack it,44,,2,
Five a Side,Soccer Game,5aside,,Advantage,28,,3,
Four Corners,Cards Game,4corn,1996,Softdisk Inc.,17,,3,
Lunattack,Action,3d_luna,,Hewson,23,,3,
Nine Princes in Amber,Adventure,9princes,1985,Telarium Corp.,320,,2,
Seven Cities Of Gold,Adventure,7cities,1984,Ozark Softscape,64,,2,
The Three Musketeers,Adventure,3_musketeers,1987,Computer Novels,164,,1,
save it in a file like TEST.CSV and process it with the program.. after some processes.. the file goes empty!

please help!

thx ..::Yuri::..
freak
PureBasic Team
PureBasic Team
Posts: 5940
Joined: Fri Apr 25, 2003 5:21 pm
Location: Germany

Post by freak »

An Array always starts with 0, so if you do "Dim array$(10000) ", you
create an array from 0-10000.
When you now fill the array$, you start at 1, so the element 0 is always
empty. When you now sort the array, this empty one is put at the end,
and the first string in the alphabet is put at position 0.
When you now write back to the file, you start at 1 again, so the string
in element 0 is lost.

Just change "pos=1" to "pos=0" everywhere.

Timo
quidquid Latine dictum sit altum videtur
Yuri
New User
New User
Posts: 8
Joined: Tue Aug 26, 2003 10:13 am
Location: Italy
Contact:

Post by Yuri »

I've made the modification.. but it continue to remove two lines from the file... :cry: :cry: :cry: :cry:

Code: Select all

Dim array$(10000)
filename$ = OpenFileRequester("Seleziona un file da ordinare in ordine ALFABETICO", "*.csv", "Comma Separated Value", 0)
If filename$="" Or filename$=".csv"
 MessageRequester("Errore", "Nessun file selezionato", #PB_MessageRequester_Ok) 
 End
EndIf
r = OpenFile(1, filename$)
If r=0
 MessageRequester("Errore", "Nome file invalido", #PB_MessageRequester_Ok) 
 End
EndIf
pos=0
eof=0
Repeat
text$ = ReadString()
If text$=""
 eof=1
 Goto fondo
EndIf
array$(pos) = text$
pos=pos+1
fondo:
Until eof=1
CloseFile(1)
quantity=pos
SortArray(array$(),2)
DeleteFile(filename$)
OpenFile(1,filename$)
pos=0
Repeat
If array$(pos)<>""
 WriteStringN(array$(pos))
EndIf
pos=pos+1
Until pos=9999
CloseFile(1)
MessageRequester("Finito", "Ordinati "+Str(quantity)+" elementi", #PB_MessageRequester_Ok) 
PB
PureBasic Expert
PureBasic Expert
Posts: 7581
Joined: Fri Apr 25, 2003 5:24 pm

Re: Please help me to figure out the problem

Post by PB »

In your original example, you just need to change this line:

Code: Select all

SortArray(array$(),2) 
to this:

Code: Select all

SortArray(array$(),2,1,pos)
The two extra flags at the end tell the SortArray command to only sort
the actual number of elements loaded, instead of the entire array. So,
1 = the first element, and pos = the last element.
Yuri
New User
New User
Posts: 8
Joined: Tue Aug 26, 2003 10:13 am
Location: Italy
Contact:

Post by Yuri »

no way.. it remove 2 lines from the text file everytime...
i've modified in this way:

Code: Select all

Dim array$(10000)
filename$ = OpenFileRequester("Seleziona un file da ordinare in ordine ALFABETICO", "*.csv", "Comma Separated Value", 0)
If filename$="" Or filename$=".csv"
 MessageRequester("Errore", "Nessun file selezionato", #PB_MessageRequester_Ok) 
 End
EndIf
r = OpenFile(1, filename$)
If r=0
 MessageRequester("Errore", "Nome file invalido", #PB_MessageRequester_Ok) 
 End
EndIf
pos=0
Repeat
text$ = ReadString()
array$(pos) = text$
pos=pos+1
Until pos=10000
CloseFile(1)
SortArray(array$(),2)
DeleteFile(filename$)
OpenFile(1,filename$)
quantity=0
pos=0
Repeat
If array$(pos)<>""
 WriteStringN(array$(pos))
 quantity=quantity+1
EndIf
pos=pos+1
Until pos=10000
CloseFile(1)
MessageRequester("Finito", "Ordinati "+Str(quantity)+" elementi", #PB_MessageRequester_Ok) 
i do not know why, but in this form only ONE line is removed from the text file everytime i sort it....
Yuri
New User
New User
Posts: 8
Joined: Tue Aug 26, 2003 10:13 am
Location: Italy
Contact:

Post by Yuri »

the modify you suggest to me reduce the loss of data from two lines to one... i think the problem is in the second part of the program (when the file is saved) but i can't figure it out...
Yuri
New User
New User
Posts: 8
Joined: Tue Aug 26, 2003 10:13 am
Location: Italy
Contact:

Post by Yuri »

"figured it out"

i've modified the first original program i posted in this way and it worked:

Code: Select all

Dim array$(10000) 
filename$ = OpenFileRequester("Seleziona un file da ordinare in ordine ALFABETICO", "*.csv", "Comma Separated Value", 0) 
If filename$="" Or filename$=".csv" 
MessageRequester("Errore", "Nessun file selezionato", #PB_MessageRequester_Ok) 
End 
EndIf 
r = OpenFile(1, filename$) 
If r=0 
MessageRequester("Errore", "Nome file invalido", #PB_MessageRequester_Ok) 
End 
EndIf 
pos=1 
eof=0 
Repeat 
text$ = ReadString() 
If text$="" 
eof=1 
Goto fondo 
EndIf 
array$(pos) = text$ 
pos=pos+1 
fondo: 
Until eof=1 
CloseFile(1) 
quantity=pos-1
SortArray(array$(),2,1,quantity) 
DeleteFile(filename$) 
OpenFile(1,filename$) 
pos=1 
Repeat 
text$ = array$(pos)
If text$<>"" 
WriteStringN(text$) 
EndIf 
pos=pos+1 
Until pos=quantity+1
CloseFile(1) 
MessageRequester("Finito", "Ordinati "+Str(quantity)+" elementi", #PB_MessageRequester_Ok) 
i do not know why, but using the debugger and a step mode i noticed a strange thing: eventually 1 or 2 lines before the end of file, at this point:
'WriteStringN(array$(pos))'
cause the program to skip the UNTIL command and go on.. i do not know why.. maybe a bug?
however using the workaround:
text$ = array$(pos)
WriteStringN(text$)
the program start to work ok!

thx to Freak and PB for the tips!
oldefoxx
Enthusiast
Enthusiast
Posts: 532
Joined: Fri Jul 25, 2003 11:24 pm

Handling ReadString() Loops

Post by oldefoxx »

Using a Repeat in order to read the contents of a file is not as good as using a While Eof(1)=0 Loop.

The Repeat assumes that you are going to execute the loop, but that would be incorrect if you have an empty file or have reached the end of the file, which is what the While Eof(1)=0 would first check for.

Anid inside your Repeat look, you are looking at an empty line as the way to tell if you have finished the file or not. Many text files have extra empty lines in them to separate text. Heck, I've got two right above this point, where I separated my paragraphs... and I am about to enter another one as well.

Of course the While command terminatges with a Wend statement, instead of the Until statement used with the Repeat. But it makes a process of this nature much easier to handle/
has-been wanna-be (You may not agree with what I say, but it will make you think).
Yuri
New User
New User
Posts: 8
Joined: Tue Aug 26, 2003 10:13 am
Location: Italy
Contact:

Post by Yuri »

the purpose of this program is to alpha-order a csv (comma separated value) file...
this file has only one empty line at the end and no empty line in the middle because there aren't paragraph... so it should work..
i found the problem in the file writing part! the file reading part was ok...
D'Oldefoxx
User
User
Posts: 30
Joined: Wed Aug 06, 2003 5:52 pm
Location: Florida, USA

Pardon Me

Post by D'Oldefoxx »

My comments were not really with regards to this specific program, but to the larger audience looking to learn how to handle data in text files. I did not try to analyze your code in terms of finding where your problem was, but just focused on your use of Repeat where I thought a While Eof(1)=0 would be somewhat more to the point. This was not meant in a critical way, but rather a small critique related to the choice of PureBASIC commands.

Perhaps I erred in doing so, but I like to encourage people to consider alternative approaches and ways of accomplishing the same end.

For instance, I like the use of SortArray very much, and think every programming language should support a Sort capability. Not all do so. But sometimes there are advantages to doing it a slightly different way. If you understand how the array iis organized in memory, usually as a series of cnsecutive memory locations, you can consider a block move approach. Each time you read a new line from a file, you search the existing contents of the array to fine where it should be placed in sequence. Then you use the block move approach to shift all elements of the array at and above that point one enemental position in the array and write the received line into the desired position, increasing the count of entries by one in the process. Of course this entails a strong understanding of how the array is structured, and tends to break down when an array of variable length strings are involved. But by creating a separate index array to support the string array, you use the index array to explore where the string should go in the string array, then you just increment all index values that are at that position and above, add the string to the end of the string array, and set as its index the position that you had determined as appropriate for it.

Some implementations of Sort can be relatively sophisticated, not only allowing you to designate an index array to use with the primary array, but even tag arrays, or whether the sort should be case sensitive or case insensitive. PowerBASIC even allows you to reweigh the sort order of characters in case you do not agree with the sequence found in the ASCII code table. You might even want the option to sort numbers-as-strings into their proper numerical sequence (right justified first) rather than the "natural" string order (left justified). In other words, a subject worth spending some time on.
Has-been Wanna-be (You may not like what I say, but it will make you think)
Post Reply