Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Pulling Data From CSV File... 2

Status
Not open for further replies.

suntsu

Programmer
Aug 26, 2001
72
0
0
I was just wondering how I could go about pulling data from a csv file, and sorting by a particular column?

I am creating a simple game, and am at the point where I need to be able to compare the current score to the top five entries recorded in a csv file.

The two fields in the csv are named Player and Score.

i just don't know how to manipulate the data, to compare and replace :(

Thanks

SunTsu
 
Why don't you try something like this. You will need to add a reference to Microsoft Scripting Runtime (SCRRUN.DLL)

This is VERY rough code:
Code:
' Put these lines in the Declarations section
Type PlayerInfo
   sPlayer as String
   nScore as Integer '(you might need a long here)
End Type

Dim FSO as new FileSystemObject
Dim TS as TextStream
Dim sPlayers as PlayerInfo

Sub DoStuff()

Dim sTemp as string
Dim sData() as string
Dim sFinal() as string
Dim p as integer

' Read the file
set TS=FSO.OpenTextFile("your csv file", ForReading)
sTemp=TS.Readall
TS.Close

' Split it into lines
sData=Split(sTemp,vbNewLine)
' sData is now an array of elements where each element relates to a single line in the original file

' Split the lines up
Redim sPlayers(lbound(sData) to Ubound(sData))
For p = lbound(sData) to ubound(sData)
   sFinal=Split(sData(p),",")
   sPlayers(p).sPlayer=sFinal(0)
   sPlayers(p).nScore=Cint(sfinal(1))
Next
' You should now have an array of sPlayers where each element contains the player name and the score.

I've just written this quickly and haven't tested it but it should give you an idea of what needs to be done.


 
Thanks WarriorPrincess,

I look forward to giving this a shot tonight.

Just one final question though, that I hope you can help with....

Once I have the array of players names, removed the lowest score and included the new high score, how can I then write this back to the csv file (overwriting the original text)?

Thanks agian for the help, much appreciated.

SunTsu
 
Simplest is usual best:

Code:
Set TS=FSo.OpenTextFile("your file name",ForWriting,True)
for p=lbound(sPlayers) to ubound(sPlayers)
   TS.WriteLine sPlayers(p).sPlayer & "," & CStr(sPlayers(p).nScore)
next
TS.Close
 
Simplest is usually best:

Code:
Set TS=FSo.OpenTextFile("your file name",ForWriting,True)
for p=lbound(sPlayers) to ubound(sPlayers)
   TS.WriteLine sPlayers(p).sPlayer & "," & CStr(sPlayers(p).nScore)
next
TS.Close
 
Hi Again :I

I tried your code WarriorPrincess, it falls at the very start where i declare the array (Type .... End Type).

I have seen similar code which leads me to believe its a problem with my setup.

I started to play around, and have ended up adding a second data control which is linked to the scoresheet csv file, and I can to a comparison between the lowest score entry (which upon detecting higher score acheived now prompts for user to enter name).

The only problem here is that I am assuming that the first record is the lowest score (rank 1st), whereas I would love to be able to...

1.sort the data controls recordset (by score column)
2.delete the record with the lowest score
3.add the new record with returned name + score
4.output full recordset to csv (I can use the code you supplied earlier :)

FYI, If it helps...

The data control is called Data2, Connect="Text;", RecordSetType="0 - Table".

Thanks again for all your help so far, it's appreciated that you take the time to help me learn :)

SunTsu
 
clarifications to earlier brain f@rt (hey, it's late :) ....

The entries to be recorded are the LOWEST five scores. Highest score deleted, record added with 'new' player name & score, recordset sorted ascending by Score, recordset array output to csv file named FlagScore.csv.

Thanks again.

SunTsu

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top