? logo basics. loading external data file to array 
Author Message
 ? logo basics. loading external data file to array

Hi all.

I'm sure this is a basic question, but I'm brand new
to logo. So... how would I load an external, white space
delimited,  data file into an array ? And, for future
reference, a 3d array?  (the file is not too big,
about 4k) ucblogo syntax is fine.

Thanks much,

Mon, 21 Jun 1999 03:00:00 GMT  
 ? logo basics. loading external data file to array


> So... how would I load an external, white space
>delimited,  data file into an array ?

This is one of those questions that leaves me wondering whether
the best response is just to answer the question as asked, or
go into related issues...  I think in this case the latter,
since you need to understand Logo lists to solve the problem
at all.

So:  Using arrays is relatively rare in Logo.  Mostly we use
lists instead; one advantage is that you don't have to say
ahead of time how big it should be.

The operation READLIST (or its abbreviation RL) will read one line
of text and return a list of the words (which can be numbers) on
that line.  The only complication is that if the line contains
square brackets, then everything within those brackets will be
one element of the list (namely a sublist).

If you want a one-dimensional data structure, that's really the
entire answer.  (If you have more than one line's worth of data
you can easily keep reading lines and appending the results.)
Since your data are in a file, you have to tell Logo to read
from that file instead of from the keyboard.  So you'd do
something like this, for the multiple-line version:

to input :filename
openread :filename
setread :filename
localmake "result input1
setread []                ; tell Logo to read from keyboard again
close :filename
output :result

to input1
if eofp [output []]
output sentence (readlist) (input1)

The parentheses in the next-to-last line aren't necessary; I just used
them to make it clearer that reading a line and calling input1
recursively are two separate things.

If you just want to read a single line, you can forget about input1
and merely say
        localmake "result readlist
before closing the file.

If what you wanted was a two-dimensional data structure, let's say
with one line of the file becoming one row, the program can be
exactly the same except that the SENTENCE in the next-to-last line
becomes FPUT, so that the list that READLIST returns for each line
becomes an element of the overall result list, rather than having
them all strung together.

If you really wanted an array (because, for example, you're going
to quicksort the numbers) the easiest thing in Berkeley Logo is to
change the line
        localmake "result input1
        localmake "result listtoarray input1
There's no direct Logo primitive for reading from a file into
an array.  (If it's a two-dimensional array you want, you'd
also have to change input1 so it says
        output fput (listtoarray readlist) (input1)
so that you end up with an array of arrays, which is how we
handle mulitple dimensions.

I hope this is neither more nore less than you wanted to know!

Tue, 22 Jun 1999 03:00:00 GMT  
 [ 2 post ] 

 Relevant Pages 

1. loading javascript arrays (or listboxes) with mysql data after a page has loaded without a refresh

2. The suitable LabVIEW data type correspinding to the array of structure in Visual Basic ActiveX DLL

3. Load data into array in ModelSIM

4. newbie - dimensioning arrays and loading data in an old program

5. DATA for loading arrays


7. Saving 2D array data to excel files( or AGL files)

8. Very basic questions about class ARRAY and FILE-types

9. Uploading Data from BASIC file Problem

10. Visual Basic Programmer wants to work on tps data files

11. loading a 2-d boolean array from a text file

12. Loading arrays and codeblocks from ini file


Powered by phpBB® Forum Software