RE: Text lines into an array of unknown size
- Posted by Matt Lewis <matthewwalkerlewis at yahoo.com> Aug 08, 2003
- 554 views
From: kjactive at adslhome.dk [mailto:kjactive at adslhome.dk] > as new commer from rexx to Euphoria I see many nice and > semilare options and a richness of WinOS options but I have some > basic newcommer problems on 'sequence a array' > I want to open a file of text of unknown size and put all lines > into an array where every line can be manipulated but I cannot get it right... First, you need to initialize tx. Euphoria does not automatically initialize variables for you, and gives an error when you try to use a variable before it has a variable. Then, rather than using tx[c] = line, you should use either append() or &= to add lines to tx. While sequences are dynamic in their size, you can't assign something beyond the sequences upper bound. I'm not sure what you're trying to do in the second printf() call that you've labeled ERROR. Here's how you can change this code: integer file object line constant ERROR = 2 integer c c = 0 sequence tx -- ADD THIS: tx = {} file = open("c:/rexx/rxcalibur.txt", "r") if file = -1 then puts(ERROR, "couldn't open text file\n") else while 1 do line = gets(file) if atom(line) then exit end if c = c + 1 -- CHANGE THIS: -- tx[c] = line -- TO THIS: tx = append( tx, line ) end while end if close(file) printf(1,"available lines:%d\n", {c}) -- correct printf(1,"%s\n",tx[2]) -- ERROR