Fun reading a file

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
bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun reading a file

Post by bjb007 » Sun Jul 13, 2008 8:24 am

I've been trying to adapt the sample script
"Open anything" to suit my project but have
files which I haven't managed to reformat the
way I need them.

Some files are like this...
4 B
7 R
21 R 1
2 B

17 B 2
19 R
32 R 3
23 R 4

and I only want the first 1 or two characters of
each line.

4
7
21 etc.

Tried all the variations I can think of
without success. Get items which include the
unwanted number on the right.

4
7
211

Have tried taking the first two characters of
each line but get an error as below - and it
doesn't make any difference whether or not
the "in" is present.

Also find that the editor isn't enabling the "Apply"
button when I remove or insert the "in".

compiling at 5:13:17 PM
Type repeat: missing 'in'
Object btnLoadNumbers
Line repeat for each line in tFiledata
Hint

Hints greatly appreciated.
Life is just a bowl of cherries.

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

Post by BvG » Sun Jul 13, 2008 8:40 am

Code: Select all

on mouseUp
repeat for each line theLine in theData
put word 1 & return after theNewData
end repeat
put char 1 to -2 of theNewData
end mouseUp
The error you got is due to a missing repeat variable:

repeat for each <chunk> <variableName> in <dataToRepeatOver>

you where not specifying <variableName> making the script non-valid.
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun reading a file

Post by bjb007 » Sun Jul 13, 2008 9:14 am

BvG

Still getting an error for "after" for some
reason.

Tried numerous combinations, renamed variables etc.

Really frustrating!

To say nothing of the editor misbehaving.
Life is just a bowl of cherries.

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

Post by BvG » Sun Jul 13, 2008 10:48 am

You still get errors because the code contains errors. Fix the code, don't get errors, it's a very simple concept :P

if you get an error complaining about "after", then something in the code before after is not correct (as you know that after is in fact allowed at that particular place). In the case of my quick example, there's a container lacking for "word". Word 1 of nothing of course won't compile, maybe you want to change the code:

Code: Select all

on mouseUp
  repeat for each line theLine in theData
    put word 1 of theLine & return after theNewData
    --put word 1 & return after theNewData
  end repeat
  put char 1 to -2 of theNewData
end mouseUp
Please read the docu entries of:
repeat (only the parts about repeat for each <chunk>)
word keyword
line keyword
after keyword
put keyword
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun reading a file

Post by bjb007 » Sun Jul 13, 2008 11:47 am

This code works...

on mouseUp
answer file "Please select a file:"
if it is empty then exit mouseUp
put URL ("file:" & it) into FileData
repeat for each line theLine in FileData
put char 1 to 2 of theLine & return after newData
end repeat
put newData into field fldText
end mouseUp

Now if only I could get something to not put
empty lines into fldText.

And if I knew what sort of hybrid is "theLine".
Looks like a variable, acts like a system variable.
Has a line of the file in it without me putting it there.
Strange.
Life is just a bowl of cherries.

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

Post by BvG » Sun Jul 13, 2008 1:44 pm

"theLine" is just a variable, but the loop sets it to a value every time (similar how "repeat with x = 1 to 10" set's the variable x).

Your version of the code will add spaces to single char numbers (zero up to nine). Use words as I suggested to not get that problem.

To not include empty lines, you can do this within the loop:

Code: Select all

if word 1 of theLine <> "" then --only add lines that contain stuff
put word 1 of theLine & return after newData
end if
don't forget to remove the last char of newData, which is a superfluous return:

Code: Select all

delete char -1 of newData
Various teststacks and stuff:
http://bjoernke.com

Chat with other RunRev developers:
chat.freenode.net:6666 #livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun reading a file

Post by bjb007 » Sun Jul 13, 2008 9:54 pm

I'm trying to add code to allow comment
lines in the files.

I have a file which starts

---Diarmais
-- system
---+225

4 B
7 R
21 R 1
2 B

When the file is opened the first line which
appears in "theLine" is

4 B

and not

---Diarmais

but the --- lines appear in the text field.

Wonder why the file appears to not be read
from the first line (according to contents of
theLine) but is according to the lines put into the field.

I want the comment lines to remain in the input
file but to filter them - but can't.

Suggestions welcome.
Life is just a bowl of cherries.

Mark
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 5150
Joined: Thu Feb 23, 2006 9:24 pm
Contact:

Post by Mark » Sun Jul 13, 2008 10:36 pm

Hi Bjb,

BvG is right, you need to use words instead of chars.

What exactly did you do, to filter out/include commented-out lines? What exactly do you want to achieve? Do you want filter out lines starting with "---" or do you want to include them?

Please, post the relevant part of your script. It'll help us to find the problem.

Best,

Mark
The biggest LiveCode group on Facebook: https://www.facebook.com/groups/livecode.developers
The book "Programming LiveCode for the Real Beginner"! Get it here! http://tinyurl.com/book-livecode

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun reading a file

Post by bjb007 » Sun Jul 13, 2008 11:03 pm

Mark

Thanks for your response as a result
of which I opened the editor to copy
the code and found (below the fold)
a spare "end mouseUp".

With that removed all's working fine
and -- lines don't appear in the output.

Strange case of cause and effect being
related in a mysterious way.
Life is just a bowl of cherries.

bjb007
Posts: 313
Joined: Fri Dec 28, 2007 4:56 am

Fun reading a file

Post by bjb007 » Mon Jul 14, 2008 4:45 pm

Just to round off this thread...

I found some files had lines where the ---
started right after the number...

23-----When

so "word 1" was "23-----When"

I used "char" and checked the ASCII code

repeat for each line theLine in FileData
put char 1 of theLine into tVar1
put char 2 of theLine into tVar2
---put char 3 of theLine into tVar3 ----for testing
---put theLine into field fldString2 ---for testing
if word 1 of theLine <> "" then
if charToNum(tVar2) > 47 and charToNum(tVar2) < 58 then
put tVar1 & tVar2 into tVar4
put tVar4 & return after newData
else
if charToNum(tVar1) > 47 and charToNum(tVar1) < 58 then
put tVar1 & return after newData
end if
end if
end if
end repeat

Not a bad bit of work for a few lines of code
and an expert could probably reduce it further.
Life is just a bowl of cherries.

Nonsanity
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 86
Joined: Thu May 17, 2007 9:15 pm
Contact:

Post by Nonsanity » Mon Jul 14, 2008 8:43 pm

Off the top of my head:

Code: Select all

repeat for each line L in Input
	put "" into LO
	repeat for each char C in L
		if C is not in "0123456789." then exit repeat -- EDIT: added . for floats
		put C after LO
	end repeat
	if LO is not empty then put LO & return after Output
end repeat
delete last char of Output
EDIT: Here's a more robust version that can collect comments, to preserve them if you want to, and this version can handle negative numbers. So what we get from our input is for any line that starts with a legal number, even if that number is butted against something else without whitespace separation...

Code: Select all

set itemdelimiter to "--"  -- Where the string is our comment marker
repeat for each line L in Input
	put item 2 to (the number of items in L) of L into Comment
	-- Strip off any comments from this line, and leading whitespace...
	put word 1 to 1000 of item 1 of L into L
	put "" into LO  -- Our ouput for this line, if any
	repeat for each char C in L
		put LO & C into LOtest  -- Make a temp var for testing
		if LOtest is a number then put LOtest into LO  -- It's good, keep it
		else exit repeat -- The next character is bad, stop collecting
	end repeat
	-- Use either of the next two lines, the second one keeps the comments
	if LO is not empty then put LO & cr after Output
	--if LO is not empty then put LO && "--" && Comment & cr after Output
end repeat
delete last char of Output
~ Nonsanity
~ Chris Innanen

Post Reply