Constantly, folks are asking for the quickest way to load a large file into an array for their needs. Usually, we start off simple by just telling them, "Use FOR BINARY, rather than FOR INPUT in your code. It saves a ton of time!"
Yet, there's always somebody who then comes along and says, "Is there a way faster than that?"
And the answer has always been: "Load it all at once and parse it."
BUT NOT ANYMORE!! I NOW BRING YOU THE LATEST, THE GREATEST, THE BESTEST NEW.....
FileToArray!!
'make a quick file for testing purposes
PRINT "Creating data file. Please wait a moment....." PRINT #1, "This is line #"; i;
". And here's my number: ";
INT(RND * 10000000) PRINT "Data file created. Now doing time tests...."
'Start the timer
FileToArray "temp.txt", lines()
PRINT USING "Loaded and parsed #,###,### lines, in ###.#### seconds."; count
, t1##
- t##
'and here's an example using an array which starts at index 1, instead of 0
FileToArray "temp.txt", lines()
PRINT USING "Loaded and parsed #,###,### lines, in ###.#### seconds."; count
, t1##
- t##
PRINT "And to compare times..." count = 1000000
REDIM lines
(1000000) AS STRING 'I'm going to cheat here and correctly size the lines array from the start ' to shave off a little time constantly resizing it, and to keep my test code down to just
' the time it takes for the program to load.
PRINT USING "Using FOR INPUT, I loaded and parsed #,###,### lines, in ###.#### seconds."; count
, t1##
- t##
REDIM lines
(1000000) AS STRING 'I'm going to cheat here and correctly size the lines array from the start 'to shave off a little time constantly resizing it, and to keep my test code down to just
'the time it takes for the program to load.
PRINT USING "Using FOR BINARY, I loaded and parsed #,###,### lines, in ###.#### seconds."; count
, t1##
- t##
FileToArray "temp.txt", lines()
PRINT USING "Using FileToArray, I loaded and parsed #,###,### lines, in ###.#### seconds."; count
, t1##
- t##
GET #FileToArray_Handle
, 1, temp$
CLOSE #FileToArray_Handle
'find CRLF
'if there's no line endings, then simply just send what we have as a single line.
count
= LBOUND(FileToArray_lines
) IF FileToArray_CRLF
= "" THEN REDIM FileToArray_lines
(count
TO count
): FileToArray_lines
(count
) = temp$:
EXIT SUB 'parse into an array
U = 1000000 + count
L
= INSTR(l1
, temp$
, FileToArray_CRLF
) FileToArray_lines
(count
) = MID$(temp$
, l1
, L
- l1
) l1
= L
+ LEN(FileToArray_CRLF
) count = count + 1
FileToArray_lines
(count
) = MID$(temp$
, l1
)
IT WORKS WITH ARRAYS DIMENSIONED FROM 0! IT WORKS WITH ARRAYS DIMENSIONED FROM 1! IT WORKS WITH ARRAYS WITH ANY SORT OF LBOUND THAT YOU CAN IMAGINE, AND THEN IT RESIZES ITSELF FROM THERE!!
Oh my Gawd!!
AND IT'S FAST! IT'S BLAZING FAST! AS THE SCREENSHOT BELOW SHOWS!!!
AND FOR A LIMITED TIME ONLY, YOU CAN GET IT FOR THE LOW, LOW PRICE OF
NOTHING, OR FOR THREE EASY PAYMENTS OF
NOTHING!!
TAKE ADVANTAGE OF THIS OFFER AND DON'T LET IT PASS YOU BY NOW!!
*Post in honor of Billy Mays Hayes -- one of the greatest salesmen ever to grace the realm of late night infomercials.
But, in all seriousness, guys, this is about as simple as it gets. REDIM an array as a string to hold your data, then simply call the sub FileToArray with the name of the file, and the name of that array, and let it load and parse it for you.
If anyone finds any little glitches, or cases where it fails to work, feel free to report them and I'll update the routine as I make corrections.
AND YES, PETE, IT
DOES WORK IN SCREEN 0!