Gamestudio Links
Zorro Links
Newest Posts
Change chart colours
by 7th_zorro. 05/11/24 09:25
Data from CSV not parsed correctly
by dr_panther. 05/06/24 18:50
AUM Magazine
Latest Screens
The Bible Game
A psychological thriller game
SHADOW (2014)
DEAD TASTE
Who's Online Now
3 registered members (AndrewAMD, degenerate_762, ozgur), 1,311 guests, and 0 spiders.
Key: Admin, Global Mod, Mod
Newest Members
Hanky27, firatv, wandaluciaia, Mega_Rod, EternallyCurious
19051 Registered Users
Previous Thread
Next Thread
Print Thread
Rate Thread
Page 1 of 3 1 2 3
Number of elements in an array? #328620
06/14/10 11:38
06/14/10 11:38
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Hello,
I have a list of numbers in an external .txt file. Is there a way to determine how many numbers are in that frile so that I can use it as a counter?

Or perhaps there is a different way to read the numbers in until I reach the end of file?

Thank you

Re: Number of elements in an array? [Re: NITRO777] #328630
06/14/10 12:31
06/14/10 12:31
Joined: Nov 2007
Posts: 1,143
United Kingdom
DJBMASTER Offline
Serious User
DJBMASTER  Offline
Serious User

Joined: Nov 2007
Posts: 1,143
United Kingdom
How are the numbers layed out in the file? One per line?

Re: Number of elements in an array? [Re: NITRO777] #328631
06/14/10 12:31
06/14/10 12:31
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
it depends on the format, are they comma seperated, are they all written as one long string, are they all written underneath each other?

maybe if you can show a piece of that, we can come up with something for you easier laugh

regards,


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: Number of elements in an array? [Re: Helghast] #328644
06/14/10 12:46
06/14/10 12:46
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Thank you very much for the reply,

They are all just one big list of numbers seperated by one space in between each one, there are no commas or other characters of any sort.

They are decimal numbers which range from say -345.67 or 66.5 ect ect

Re: Number of elements in an array? [Re: NITRO777] #328648
06/14/10 13:27
06/14/10 13:27
Joined: Jan 2004
Posts: 3,023
The Netherlands
Helghast Offline
Expert
Helghast  Offline
Expert

Joined: Jan 2004
Posts: 3,023
The Netherlands
cool, you can use a while loop to count the number of times file_find comes across a space in that file.
That will give you the amount of numbers in the list.

regards,


Formerly known as dennis_fantasy
Portfolio - http://www.designorhea.com/
Project - http://randomchance.cherrygames.org/
Re: Number of elements in an array? [Re: Helghast] #328651
06/14/10 13:35
06/14/10 13:35
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Excellent idea thank you.

Re: Number of elements in an array? [Re: NITRO777] #328682
06/14/10 16:04
06/14/10 16:04
Joined: Mar 2009
Posts: 88
Walori Offline
Junior Member
Walori  Offline
Junior Member

Joined: Mar 2009
Posts: 88
there would be another way also, incase none of the numbers is 0 exactly. Just put a file_var_read to the while loop and increase counter amount on each loop call. When 0 is reached close the loop.something like this:

Code:
var var_to_read = 0;
var var_amount = 0;
var handle_of_file = file_open_read("FILE.txt");
while(1)
{
   var_to_read = file_var_read(handle_of_file);
   if(var_to_read == 0)
   {
       break;
    }
   var_amount++;
}



Last edited by Walori; 06/14/10 16:05. Reason: code brackets!
Re: Number of elements in an array? [Re: Walori] #328684
06/14/10 16:31
06/14/10 16:31
Joined: Mar 2003
Posts: 3,010
analysis paralysis
NITRO777 Offline OP
Expert
NITRO777  Offline OP
Expert

Joined: Mar 2003
Posts: 3,010
analysis paralysis
Hi Walori,
So basically your method will work if none of the numbers in the list are 0, right? In my particular case many numbers in the file could be zero, so I guess I cannot use this approach. This was originally the method I had thought of before asking the forum. Anyway, thank you very much for the reply. I appreciate the effort.

Re: Number of elements in an array? [Re: NITRO777] #328731
06/14/10 22:01
06/14/10 22:01
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Untested auto enf-of-file checking
Code:
var var_to_read = 0;
var var_counter = 0;
var handle_of_file = file_open_read("FILE.txt");
...
while(1)
{
   var_to_read = file_var_read(handle_of_file);
   if(file_seek(handle_of_file,0,4) >= (file_length(handle_of_file)-0))
   {
       break;
    }
   var_counter++;
}


Because untested, the IF may need to check against
(file_length(handle_of_file)-1)
or
(file_length(handle_of_file)-2) or even -3, depending if file ends with a line-feed or not.

let us know how it goes...


"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Re: Number of elements in an array? [Re: EvilSOB] #328732
06/14/10 22:07
06/14/10 22:07
Joined: Feb 2008
Posts: 3,232
Australia
EvilSOB Offline
Expert
EvilSOB  Offline
Expert

Joined: Feb 2008
Posts: 3,232
Australia
Also, seeing as you know the number-range, you could put the last number
WELL outside the possible range, to act as an end-of-numbers marker.

eg (untested again)
Code:
#define EOF -999999          //end-of-file value
var var_to_read = 0;
var var_counter = 0;
var handle_of_file = file_open_read("FILE.txt");
...
while(1)
{
   var_to_read = file_var_read(handle_of_file);
   if(var_to_read == EOF )  {   break;   }
   var_counter++;
}




"There is no fate but what WE make." - CEO Cyberdyne Systems Corp.
A8.30.5 Commercial
Page 1 of 3 1 2 3

Moderated by  HeelX, Lukas, rayp, Rei_Ayanami, Superku, Tobias, TWO, VeT 

Gamestudio download | chip programmers | Zorro platform | shop | Data Protection Policy

oP group Germany GmbH | Birkenstr. 25-27 | 63549 Ronneburg / Germany | info (at) opgroup.de

Powered by UBB.threads™ PHP Forum Software 7.7.1