Questions re: URL read/write and if-then

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

Questions re: URL read/write and if-then

Post by ittarter » Wed Jun 24, 2015 11:58 am

Hi, resident dunce here. Here's another two questions I have.

1. I'm trying to load existing user data into some variables. However, when I run this command, even though the program correctly says that the URL exists, it can't "put" data from the URL into a field or variable. I was under the impression that I could read and write files with the URL command.

2. I'm having unexpected problems with my if-then statements. When I apply changes to my card script I get the error "Handler: not a command" on the line "else". I've tried several variations and the only one that doesn't produce an error is if I begin each line in the if-then sequence with "then". However, none of the examples I've seen in the Dictionary or in any online tutorials ever does this. Why is mine behaving differently?

Code: Select all

if there is a URL ("file:" & gName & ".txt") then put line 2 of URL ("file:" & gName & ".txt") into gLanguage
   put line 1 of URL ("file:" & gName & ".txt") into fld "userdata"
   answer "User data loaded"
else answer "We cannot find your user data. Please set folder and try again."
setfolder
end if
I should add that I have learned a great deal from previous responses on this forum and feel like I've made huge steps forward :D

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

Re: Questions re: URL read/write and if-then

Post by Klaus » Wed Jun 24, 2015 12:29 pm

Hi ittarter,

1. there shouldn't be any problem with reading and writing files with the URL syntax!
Unless you try to write in folders where you do not have write permissions!

You should check the result after you try to read or write something and it fails.
The result = EMPTY on success!

Code: Select all

...
put url("file:xcxcxcx.txt") into tWhatever

## The result may give a hint about what goes wrong if it goes wrong:
if the result <> empty then
   answer the result
end if
...
2. I always use the correct and maybe longer snytax with IF THEN and never had problems this way,
which will also intend correctly and nicely when you hit TAB in the script editor:

Code: Select all

...
## Save some typing:
   put ("file:" & gName & ".txt") into tFileURL
   if there is a URL (tFileURL) then 
      put line 2 of URL (tFileURL) into gLanguage
      put line 1 of URL (tFileURL) into fld "userdata"
      answer "User data loaded"
   else
      answer "We cannot find your user data. Please set folder and try again."
      setfolder
   end if
...
Best

Klaus

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

Re: Questions re: URL read/write and if-then

Post by ittarter » Wed Jun 24, 2015 2:24 pm

Thank you, Klaus! I still can't get it to work when I try to put the entire URL into a single variable, so I have to type out the combination each time, but... it works!

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

Re: Questions re: URL read/write and if-then

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

What does THE RESULT say when you try that?

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10052
Joined: Sat Apr 08, 2006 7:05 am
Contact:

Re: Questions re: URL read/write and if-then

Post by FourthWorld » Wed Jun 24, 2015 4:30 pm

In addition to "the result" with file I/O the "sysError" function can be very helpful to let the OS inform you of the exact reason the file can't be read.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Questions re: URL read/write and if-then

Post by ittarter » Thu Jun 25, 2015 12:32 am

Klaus wrote:What does THE RESULT say when you try that?
Nothing, as it should. I'll play around with it a bit more to see if I can come up with any other result.

Post Reply