Count number of lines in file.

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
Pedro Prevost
Posts: 14
Joined: Sun Mar 24, 2013 5:24 pm
Contact:

Count number of lines in file.

Post by Pedro Prevost » Sun Mar 24, 2013 7:50 pm

Hi all,


Just started programming with Livecode and really enjoying myself. My first application is a proof of concept for a small and cheap data com app to be used in a taxi system. It took me about a day to get things running but one line of code keeps giving me a lot of problems,

get the number of lines of URL "http: // www mobiportal nl/ritten.txt"
put it into cUbound

The file to be read is on our web server and its contents is al list of trips for a specific day. In order to loop through the file, and make choices as enable next button etc, i need the number of lines in the file. The code above the line functions about 40% of the times its called an gives back 189, the correct number of lines in the file. The other 60% it returns a 0. I have read some forum articles about networking problems and test with some code changes but no result.

Anyone a suggestion where i, or LC are making an error?



Thanks,



Pedro Prevost

sturgis
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 1685
Joined: Sat Feb 28, 2009 11:49 pm

Re: Count number of lines in file.

Post by sturgis » Sun Mar 24, 2013 8:15 pm

When it returns 0, you might check to see what is in "the result" in case there is an error listed there.
Also you might use load instead of get (if an asynchronous load is ok with the way you're putting your program together) that way you can use:

load URl "http://whatever.url/file.text" with message urlLoaded

Then have a urlLoaded command to catch the results when the load is complete

command urlLoaded pUrl,pStatus
get the number of lines in url pUrl -- since the url is already loaded into cache its instantaneous
unload url pUrl -- unload the url again to remove it from cache and ensure a fresh load of the data next time
end urlLoaded

YOu can also look to see whats in pStatus to check for errors


There are some differences between how load works on the desktop vs how it works on mobile. This example is for the desktop.

On mobile the actual data grabbed during the load is passed to the handler you set up as the "with message.." so that rather than getting the number of lines that are in URL pUrl you'd just get the number of lines in the passed variable.

So, instead of command urlLoaded pUrl,pStatus it would be urlLoaded pUrl,pStatus,pData where pData would actually contain the contents of the downloaded url. And on mobile, no need to unload the url since it is not kept in cache. (you can have the 3rd parameter on desktop and just ignore it, and code things do that the correct behavior occurs based on the device/os that is executing)

Pedro Prevost
Posts: 14
Joined: Sun Mar 24, 2013 5:24 pm
Contact:

Re: Count number of lines in file.

Post by Pedro Prevost » Sun Mar 24, 2013 8:42 pm

Sturgis,

Tested your code and now the result is 100% correct. Will implement it tomorrow in the application to see how it works, and if i fully understand whats happpening.


Thanks for the swift reply,


Pedro

Post Reply