Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / PRINTING numbers from within a file in NUMERICAL ORDER

Author
Message
Mega Publishers
16
Years of Service
User Offline
Joined: 17th Dec 2007
Location:
Posted: 2nd Jul 2008 03:46
I have a text file named SCORES.txt with 10 different numbers inside the file.

I know how to have dark basic pro read and print the contents of the file to the screen, but I want the Numbers within the text file to be printed to the screen in NUMERICAL ORDER, as in, the highest number printed first, then the second highest number, and so on. How do I do this?
MSon
20
Years of Service
User Offline
Joined: 13th Jul 2004
Location: Earth, (I Think).
Posted: 2nd Jul 2008 12:37 Edited at: 2nd Jul 2008 12:41
Theres many ways to do this, but i think this is proberley one of the simplest, in the example below, you should be able to access the dim called "Scores" once the Function has completed, i've not tested it, but it should work.



Everyone Be Cool, You, Be Cool.
Mega Publishers
16
Years of Service
User Offline
Joined: 17th Dec 2007
Location:
Posted: 2nd Jul 2008 17:04
I got a error message when I ran this code.
sinisterstuf
17
Years of Service
User Offline
Joined: 28th Mar 2007
Location: Namibia, Southern Africa
Posted: 3rd Jul 2008 03:04
What kind of error message?
Basically MSon has read all the numbers to an array. He then checks if a value in the array is greater than the previous one. If so then they are switched and so the array values are rearranged from largest to smallest, if I understand correctly.

When the going get stuf,
the stuf get's sinister
RedFlames
17
Years of Service
User Offline
Joined: 25th Aug 2007
Location: Germania
Posted: 3rd Jul 2008 13:38 Edited at: 3rd Jul 2008 13:38
I think there is no command called "read integer", but you could use "read file" or "read long"...

Mega Publishers
16
Years of Service
User Offline
Joined: 17th Dec 2007
Location:
Posted: 3rd Jul 2008 16:01
When I run this code, it highlights the

READ Integer 1, line and says

"Could not determine parameter type of "Integer 1".

When I change Read Integer to Read String, I get

"Parameter for 'READ STRING' do not match File Number Variable String

These are the two error messages I get.
sinisterstuf
17
Years of Service
User Offline
Joined: 28th Mar 2007
Location: Namibia, Southern Africa
Posted: 3rd Jul 2008 19:29
I know no READ INTEGER command but assuming that you saved the scores as integers then:
READ INTEGER must be changed to READ BYTE
however, if they are saved as strings, which you might also have done, then the reason READ STRING command gives an error is probably because the array was not defined as string. This means you can either define the array as string using:
DIM scores$(10) AS STRING
but this is inadvisable becuase you cannot do calculations with strings which means that you'll probably have to convert them to integers each time. The other (recommended) option is to leave the array as integer type and convert the strings your highscore file into integers as they come using:



I'm not sure if val() returns integers or floats so I put both val() and int() just incase. The help file is very unclear about this.

Another possibility is that you saved the scores as an array then you can just load the array using the
LOAD ARRAY(0) filename$
command

was this helpful?

When the going get stuf,
the stuf get's sinister

Login to post a reply

Server time is: 2024-11-25 21:47:15
Your offset time is: 2024-11-25 21:47:15