Select a level - search txt file for corresponding string

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

Post Reply
ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Select a level - search txt file for corresponding string

Post by ittarter » Wed Jun 17, 2015 2:35 pm

Hi!

I'm designing a program to help people practice speaking in other languages. I'm stuck on the second card, where they select which level they want to play. I was planning to have all the levels in a single text file (see below).
1. I want the user to enter which level they want to play,
2. then for the app to find the line in levels.txt with the corresponding level code (e.g. FrB201)
3. for the line number to be stored for future reference,
4. and finally, for the items on that line to be copied to a data grid.

However, I'm getting errors "chunk: no such object" and "Function: error in function handler" on the bolded lines below, respectively. I've tried about a dozen different variations and have read these three lessons repeatedly (can't refer you to them with proper links because my account on this forum is still pretty new, I guess):
44093-reading-and-writing-to-file
44092-Working-with-text
9849-how-can-i-search-delimited-data-for-a-matching-string

However, I haven't had any success to date. Any suggestions or other resources? Thanks!

Code: Select all

on loadlevels
   global gLevel, gStartLine
      put empty into field fld.leveldata
      if there is a file "levels.txt"
         read from file ("levels.txt") until EOF
         put it into field fld.leveldata
      else 
         beep
      end if
      ask "Which level do you want to try? e.g. FrB201"
      put it into gLevel
[b]      put lineoffset (gLevel, field fld.leveldata,0) into gStartLine
      put line gStartline of field fld.leveldata into field grid.userdata[/b]
end loadlevels
levels.txt currently looks like this:

FrB201,177,Le voyage d'affaires,5
Did you have a nice trip?
How was your flight?
Where are you staying?
Is your hotel alright?
How long are you staying in London?

FrB202,178,S'excuser,5
I'm sorry for being late.
Please accept my apologies.
I was detained by a meeting.
There's a lot of traffic today.
Have you been waiting long?

EnA101,1,Say your nationality,5
Je suis français.
Etes-vous américaine ?
Non, je ne suis pas américaine.
Je suis français.
Je m'appelle François.

EnA101,2,Say your country/city,5
Je suis de la France.
Etes-vous de l'Irelande ?
Oui, je suis d'Irelande.
Comment allez-vous ?
Bien, merci, et vous ?

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Select a level - search txt file for corresponding strin

Post by dunbarx » Wed Jun 17, 2015 2:40 pm

Hi.

You simply must learn to quote literals:

put empty into field "fld.leveldata"

Which line throws the error?

Craig Newman

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Select a level - search txt file for corresponding strin

Post by ittarter » Wed Jun 17, 2015 3:16 pm

dunbarx wrote:Hi.

You simply must learn to quote literals:

put empty into field "fld.leveldata"

Which line throws the error?

Craig Newman
So, a literal is an object? OK, I will start quoting them.

Sorry, the bold code didn't trigger when imbedded in the code block. There are two lines that cause errors:

put lineoffset (gLevel, field fld.leveldata,0) into gStartLine
# error = chunk: no such object
put line gStartline of field fld.leveldata into field grid.userdata
# error = Function: error in function handler

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10331
Joined: Wed May 06, 2009 2:28 pm

Re: Select a level - search txt file for corresponding strin

Post by dunbarx » Wed Jun 17, 2015 3:43 pm

Hi.
So, a literal is an object? OK, I will start quoting them.
Well, yes, but literals can just be, er, literals:

Code: Select all

put "thisTextIsJustALiteral" into fld "yourFieldThatHoldsLiterals"
When you examine the variable pane at the error line, what are the values of the various components of that line? for example, what is in field fld.leveldata (sic). There may be an issue with the quote thing. If you have a field named "myField", and a button with this in its script:

Code: Select all

on mouseUp
   put 33 into myField
   put 44 into fld myField
end mouseUp
You will get the sort of error you are being chastised about. Now try this in the button script:

Code: Select all

on mouseUp
   put 33 into myField
   put 44 into fld "myField"
end mouseUp
No error. Do you see why quoting properly is critical? LC does it best with sloppy coding, but it is not a mind reader.

Craig

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

Re: Select a level - search txt file for corresponding strin

Post by Klaus » Wed Jun 17, 2015 3:48 pm

Hi ittarter
ittarter wrote:So, a literal is an object?
no, the NAME of an object is a literal! 8)
field "my field"
button "my nice button"
etc...

When using "read from file..." you need to OPEN that file (for read) first and close it afterwards!
I use the URL syntax, which is much shorter:

Code: Select all

...
put empty into field "fld.leveldata"

## missing THEN in your script!
if there is a file "levels.txt" THEN
   put url("file:level.txt") into fld "fld.leveldata"
else
  beep
end if
...
Best

Klaus

ittarter
Posts: 151
Joined: Sat Jun 13, 2015 2:13 pm

Re: Select a level - search txt file for corresponding strin

Post by ittarter » Fri Jun 19, 2015 3:56 pm

Thanks for the feedback on quoting literals. It doesn't fully solve the problem. I'll give an update as soon as I can.

Post Reply