URGENT! Need to read a text file line by line

LiveCode is the premier environment for creating multi-platform solutions for all major operating systems - Windows, Mac OS X, Linux, the Web, Server environments and Mobile platforms. Brand new to LiveCode? Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Post Reply
user#606
Posts: 217
Joined: Sun Jan 27, 2008 12:25 pm
Contact:

URGENT! Need to read a text file line by line

Post by user#606 » Sun Apr 20, 2008 10:19 am

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)

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sun Apr 20, 2008 10:54 am

Hi user#606,

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"
...
Hope that helps.


Best

Klaus

user#606
Posts: 217
Joined: Sun Jan 27, 2008 12:25 pm
Contact:

Post by user#606 » Sun Apr 20, 2008 2:20 pm

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

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sun Apr 20, 2008 4:11 pm

user#606 wrote:Dear Klaus,
Thank you for the code and I tried it, but without success.
Hm, I use this syntax everyday and KNOW it does work :-)
What exactly did you script? Could you post yor script please?
Did you get an error? If yes, what?
user#606 wrote:Basically it would not read the file. I checked the address and it appears ok.
I saw that you use the Windows path delimiter backslash ""!
Rev uses the UNIX slash delimiter "/"!
Maybe that prevents the file from being read.
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.
Yes, but that depends on the size of the files!
I think that one or two MBs will work fine on slower PCs.
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.
The "repeat for each ..." loop will process every line until all lines have been processed.
No need to check for EOF in this case ;-)
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?
See above, at first you need to use "/" as path delimiter:
"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

BvG
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 1239
Joined: Sat Apr 08, 2006 1:10 pm
Contact:

Post by BvG » Sun Apr 20, 2008 5:22 pm

from the docu:
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.
Therefore:

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
things to look up in the documentation:
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

user#606
Posts: 217
Joined: Sun Jan 27, 2008 12:25 pm
Contact:

Different problems this time.

Post by user#606 » Sun Apr 20, 2008 8:32 pm

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.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Sun Apr 20, 2008 8:55 pm

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

user#606
Posts: 217
Joined: Sun Jan 27, 2008 12:25 pm
Contact:

Post by user#606 » Mon Apr 21, 2008 8:55 am

Thank you Klaus, you have helped a great deal and I do appreciate it.

I think this segment of the programme is nearly up an running.

The different cards will folllow a similar layout for the various construction types, so once I have the pattern, the differences will be easy to accommodate.

Klaus
Posts: 14194
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Post by Klaus » Mon Apr 21, 2008 8:57 am

At your services, Sire! :-)

Post Reply