
-----------------------------------
smool
Wed May 23, 2012 12:21 pm

File reading
-----------------------------------
I have a file with over 5000 names, and I have to read them all and put it into an array. The problem is that the text file doesnt have the names on seperate lines, or even seperated by spaces. A sample of part of the file:

"MARY","PATRICIA","LINDA","BARBARA","ELIZABETH","JENNIFER","MARIA"

I try to use the usual get command, but it tries to get the whole line since there are no spaces of line breaks, and so I get an error "Input string to large for string variable".

Is there any way to get around this? to get each name individually?

Oh, and yes this is for project Euler, problem #22. I feel like it's acceptable to ask for help on this part since this isn't actually what the problem is about.

-----------------------------------
Tony
Wed May 23, 2012 12:26 pm

RE:File reading
-----------------------------------
Turing strings are limited to 255 characters in size (I think)... definitely less than 5000 names. See [tdoc]get[/tdoc] about reading in one character at a time.

-----------------------------------
Raknarg
Wed May 23, 2012 1:46 pm

RE:File reading
-----------------------------------
1. Open the textfile
2. Go Edit > Replace
3. ???
4. Profit

-----------------------------------
Aange10
Wed May 23, 2012 5:45 pm

RE:File reading
-----------------------------------
Try something like


var names : flexible array 0 .. 0 of string
names (0) := ""
loop
    exit when eof(stream)
    if  upper(names) not= 0 or names (0) not= "" then
         new names, upper(names) + 1
    end if
    get : stream, names (upper(names)) : 255
end loop


-----------------------------------
Amarylis
Wed May 23, 2012 8:57 pm

RE:File reading
-----------------------------------
This worked for me in small scale (tried with the 7 words you gave me and it worked out fine), Idk how well it'll do for 5000 names


var bigName : string
var name : flexible array 0 .. -1 of string
var st, endSt : int
var filestream : int
st := 1

open : filestream, "file.txt", get

loop
    exit when eof (filestream)
    get : filestream, bigName : 255
    loop
        exit when st >= length (bigName)
        endSt := st + index (bigName (st + 1 .. *), '"') - 1
        if bigName (st + 1 .. endSt) ~= "," then
            new name, upper (name) + 1
            name (upper (name)) := bigName (st + 1 .. endSt)
            put name (upper (name))
        end if
        st := endSt + index (bigName (endSt + 1 .. *), '"')
    end loop
end loop


-----------------------------------
smool
Wed May 23, 2012 10:17 pm

RE:File reading
-----------------------------------
Okay thanks everyone, I managed to solve it :D

-----------------------------------
Raknarg
Thu May 24, 2012 8:29 am

RE:File reading
-----------------------------------
"This is impossible!"
"Have you tried replacing?"
"Oh..."
