Source language: Translate to:

Parsing is better than reading

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

Parsing is better than reading

Postby Dorjee » Wed Sep 08, 2010 1:36 am

Not a question but a trick for the community (if necessary).
If for any reason you need to put a big file (say several 100 lines), line by line, in an array you can use a "reading" approach like this:

Code: Select all
. find number of lines in the file
   FileLen "MyFile.txt" "[NbLines]"
. loop the number of lines
   Loop "1" "[NbLines]" "[i]"
. read the line number [i]
   FileRead "MyFile.txt" "[i]" "[LineContent]"
. fill the array
   SetVar "[MyArray[i]]" "[LineContent]"
. and loop and loop ...

The problem in this method is that there is an access to the hard disk for each line. Knowing that this reading takes time (even it is a part of a second), in the end, multiplicating this time by the number of lines that gives a ... long time for the user waiting for the process is finished. Another more quick and easy method is possible: parsing.

Code: Select all
. put the entire file in one variable
   FileToVar "MyFile.txt" "[MyFile]"
. parse this variable content in the array (that's all folks!)
   StrParse "[MyFile]" "[#13][#10]" "[MyArray]" "[NbLines]"

The method is using only two lines of codes. With FileToVar action there is only one reading in the hard disk, and then the work is not between hard disk and computer RAM, but in RAM only. With StrParse action you put each line of the file content (in the FileToVar variable) in a cell of the array using the end-of-line characters [#13][#10] as the parsing condition. Note that NbLines is given by this second action too.
Dorjee the Neobooker
Posts: 27
Joined: Thu Nov 17, 2005 12:15 am

Postby Neosoft Support » Wed Sep 08, 2010 10:22 am

Thanks for sharing this with us Dorjee!
NeoSoft Support
Neosoft Support
NeoSoft Team
Posts: 5593
Joined: Thu Mar 31, 2005 10:48 pm
Location: Oregon, USA

Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 2 guests