URGENT! Need to read a text file line by line
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
URGENT! Need to read a text file line by line
This basic process is mentioned in many places, but not set out as a workable piece of code, perhaps it is so obvious, I cannot understand it.
Text file called material.txt
Contains data in the following format:-
Masonry (TAB) Plaster Board (TAB) 100mm Insulation Batts (END OF LINE)
Brick (TAB) 100mm Block(TAB) 100mm Celotex Insulation (END OF LINE)
and so on...
I cannot find how to open the file, read in first line, putting the TAB delimited data into Var1 Var2 Var3
Then process the Var1,Var2,Var3 (no problem with that)
And so on with the next line etc. until EOF
Close file
The purpose is to filter the lines of data so only selected parameters are displayed in a table. (no problem with that either)
Text file called material.txt
Contains data in the following format:-
Masonry (TAB) Plaster Board (TAB) 100mm Insulation Batts (END OF LINE)
Brick (TAB) 100mm Block(TAB) 100mm Celotex Insulation (END OF LINE)
and so on...
I cannot find how to open the file, read in first line, putting the TAB delimited data into Var1 Var2 Var3
Then process the Var1,Var2,Var3 (no problem with that)
And so on with the next line etc. until EOF
Close file
The purpose is to filter the lines of data so only selected parameters are displayed in a table. (no problem with that either)
Hi user#606,
try this:
Hope that helps.
Best
Klaus
try this:
Code: Select all
...
## With this one-liner you do not need ot open/read/close, less typing :-)
put url("file:" & "path/to/your/material.txt") into any_var
## Set itemdelimiter:
set itemdel to TAB
## Process each line of the variable:
repeat for each line tLine in any_var
put item 1 of tLine into var1
put item 2 of tLine into var2
put item 3 of tLine into var3
## Pass these vars to any function that will do your processing:
## put my_function(var1) into processed_var1
## etc...
## Collect the processed data:
put processed_var1 & TAB & processed_var2 & TAB & processed_var3 & CR after processed_content
end repeat
## get rid of trailing CR
delete char -1 of processed_content
## Put it into a list field or whatever, you get the picture
put processed_content into fld "my display field"
...
Best
Klaus
Dear Klaus,
Thank you for the code and I tried it, but without success.
Basically it would not read the file. I checked the address and it appears ok.
However, In this program, there will be a large number of data files open and I believe reading, using url takes in the whole file rather than just a line at a time. I have to assume the user will have a low spec pc, so memory usage is an important criteria.
Also, I could not see how the repeat knows when the end of the data is reached. This is the problem I have with reading line by line from the data file. I cannot think of a way of using EOF or something similar.
I have shown below the code I have developed. It uses my preferred method.
How can I stop the file read at end of file?
on mouseUp
open file "C:\TMPL\cavitywall.txt"
repeat until materials = "end" -- the word end is the last line of the data in file. I need to find out how to use EOF to stop the loop
read from file "C:\TMPL\cavitywall.txt" for 1 line
put it into materials -- just so I have a sensible variable to work with
answer materials -- to check how things are progressing in the read process
--- This is added for testing, because I have not found out how to use EOF to stop the loop
add 1 to loop
if loop = 10 then put "end" into materials
end repeat
close file "C:\TMPL\cavitywall.txt"
end mouseUp
Thank you for the code and I tried it, but without success.
Basically it would not read the file. I checked the address and it appears ok.
However, In this program, there will be a large number of data files open and I believe reading, using url takes in the whole file rather than just a line at a time. I have to assume the user will have a low spec pc, so memory usage is an important criteria.
Also, I could not see how the repeat knows when the end of the data is reached. This is the problem I have with reading line by line from the data file. I cannot think of a way of using EOF or something similar.
I have shown below the code I have developed. It uses my preferred method.
How can I stop the file read at end of file?
on mouseUp
open file "C:\TMPL\cavitywall.txt"
repeat until materials = "end" -- the word end is the last line of the data in file. I need to find out how to use EOF to stop the loop
read from file "C:\TMPL\cavitywall.txt" for 1 line
put it into materials -- just so I have a sensible variable to work with
answer materials -- to check how things are progressing in the read process
--- This is added for testing, because I have not found out how to use EOF to stop the loop
add 1 to loop
if loop = 10 then put "end" into materials
end repeat
close file "C:\TMPL\cavitywall.txt"
end mouseUp
Hm, I use this syntax everyday and KNOW it does workuser#606 wrote:Dear Klaus,
Thank you for the code and I tried it, but without success.

What exactly did you script? Could you post yor script please?
Did you get an error? If yes, what?
I saw that you use the Windows path delimiter backslash ""!user#606 wrote:Basically it would not read the file. I checked the address and it appears ok.
Rev uses the UNIX slash delimiter "/"!
Maybe that prevents the file from being read.
Yes, but that depends on the size of the files!user#606 wrote:However, In this program, there will be a large number of data files open and I believe reading, using url takes in the whole file rather than just a line at a time. I have to assume the user will have a low spec pc, so memory usage is an important criteria.
I think that one or two MBs will work fine on slower PCs.
The "repeat for each ..." loop will process every line until all lines have been processed.user#606 wrote:Also, I could not see how the repeat knows when the end of the data is reached. This is the problem I have with reading line by line from the data file. I cannot think of a way of using EOF or something similar.
No need to check for EOF in this case

See above, at first you need to use "/" as path delimiter:user#606 wrote:I have shown below the code I have developed. It uses my preferred method.
How can I stop the file read at end of file?
"C:/TMPL/cavitywall.txt"
Then you can:
...
repeat until EOF
...
Please check the docs for "EOF" and "read from file..."!
Hope that helps.
Best
Klaus
from the docu:
things to look up in the documentation:
open file
read from file
repeat
eof
answer file
Therefore:If the read from file command encounters the end of the file, the result function is set to "eof". If the command was successful and did not encounter the end of the file, the result function is set to empty.
Code: Select all
on mouseUp
answer file "some text file"
if it = "" or the result <> "" then
exit mouseUp
end if
put it into theFile
open file theFile for read
set the itemdelimiter to tab
repeat until theQuestion = "eof"
read from file theFile until return
put the Result into theQuestion
put it into theLine
put item 1 of theLine into var1
put item 2 of theLine into var2
put item 3 of theLine into var3
--do your own stuff here
end repeat
close file theFile
end mouseUp
open file
read from file
repeat
eof
answer file
Various teststacks and stuff:
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
http://bjoernke.com
Chat with other RunRev developers:
chat.freenode.net:6666 #livecode
Different problems this time.
Dear Klaus and BvG
Thank you for your help. It is interesting how the same problem can have so many different answers.
My working solution is different again.
I have a further problem, but involves grouped radio buttons.
The group is called Name group id 2037 and consists of 4 radio buttons vertically. The user will select a button and I need to determine which has been selected.
I have even tried to set a button to get some clue as to what is functional, but nothing works.
Can you help with that?
If you want to see the programe I can send it, if it helps.
Thank you for your help. It is interesting how the same problem can have so many different answers.
My working solution is different again.
I have a further problem, but involves grouped radio buttons.
The group is called Name group id 2037 and consists of 4 radio buttons vertically. The user will select a button and I need to determine which has been selected.
I have even tried to set a button to get some clue as to what is functional, but nothing works.
Can you help with that?
If you want to see the programe I can send it, if it helps.
Hi user#666,
This is an easy one
You can retrieve the number of the radio button clicked or its name:
...
put the hilitedbutton of grp "your radiobutton group here" into number_clicked
## Note: This will return the number relative to the group!
...
OR
...
put the hilitedbuttonname of grp "your radiobutton group here" into name_of_clicked_button
...
Hint: You should give your groups (and objects) meaningful names, this way it is easier to read and understand the code AND to address your objects!
Example: group "My radiobutton group 1" vs. group id 2037
Hope that helps.
Best
Klaus
This is an easy one

You can retrieve the number of the radio button clicked or its name:
...
put the hilitedbutton of grp "your radiobutton group here" into number_clicked
## Note: This will return the number relative to the group!
...
OR
...
put the hilitedbuttonname of grp "your radiobutton group here" into name_of_clicked_button
...
Hint: You should give your groups (and objects) meaningful names, this way it is easier to read and understand the code AND to address your objects!
Example: group "My radiobutton group 1" vs. group id 2037

Hope that helps.
Best
Klaus