Fastest way to know the contents of the latest line of a doc

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
danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

Fastest way to know the contents of the latest line of a doc

Post by danielrr » Wed Apr 26, 2017 2:22 pm

Hi all,

The subject of the message says almost all: What's the fastest way in LiveCode to know the contents of the latest line of a very long text document? And not just the last line, but the nth line?

best,

Daniel

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Fastest way to know the contents of the latest line of a

Post by mrcoollion » Wed Apr 26, 2017 2:31 pm

Put the text in a field or variable (in this case a field named "NameOfTheFieldWithTheText").

Put the below code in a button and voila...

Code: Select all

on mouseUp
    put line 9 of fld "NameOfTheFieldWithTheText" into tText
    answer tText
    put the last line of of fld "NameOfTheFieldWithTheText" into tText
    answer tText
end mouseUp
Regards,

Paul

danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

Re: Fastest way to know the contents of the latest line of a

Post by danielrr » Wed Apr 26, 2017 2:40 pm

I mentioned "long" document to point to the case where the contents of the file are too long to be copied in memory. Of course you can just read the whole file and return the last line or the nth line. But in cases when such procedure is not advisable due to memory limitations, what's the fastest way to do it?

best,

Daniel

mrcoollion
Posts: 720
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Fastest way to know the contents of the latest line of a

Post by mrcoollion » Wed Apr 26, 2017 3:04 pm

Try this..

Code: Select all

on mouseUp
    answer file "Please choose the file you want to work with"
    put it into tFile
    if there is a file tFile then 
        put tFile into tDocumentPathAndName
    else
        answer "No file captured"
        exit mouseup
    end if
    get line 9 of URL ("file:" & tDocumentPathAndName) 
    put it into tText
    answer tText
    get the last line of URL ("file:" & tDocumentPathAndName) 
    put it into tText
    answer tText
end mouseUp
Tested it with a txt filetype.

Regards,

Paul

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Fastest way to know the contents of the latest line of a

Post by dunbarx » Wed Apr 26, 2017 5:16 pm

Mr. Coollion's method seems pretty straightforward.

But just curious; what kind of document are you thinking of? The text portion of the Encyclopedia Britannica is only 100MB or so (my estimate). That is not even close to the memory limits of the usual computers these days.

Craig Newman

danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

Re: Fastest way to know the contents of the latest line of a

Post by danielrr » Wed Apr 26, 2017 7:55 pm

Code: Select all

get the last line of URL ("file:" & tDocumentPathAndName) 
Woah! Thanks. That's even better that I had expected.
The only problem with this kind of Wunder answers is that they make me wonder how much I'm still missing. By the way, how does *that* works?

best,

Daniel

danielrr
Posts: 140
Joined: Mon Mar 04, 2013 4:03 pm

Re: Fastest way to know the contents of the latest line of a

Post by danielrr » Wed Apr 26, 2017 8:01 pm

dunbarx wrote:Mr. Coollion's method seems pretty straightforward.

But just curious; what kind of document are you thinking of? The text portion of the Encyclopedia Britannica is only 100MB or so (my estimate). That is not even close to the memory limits of the usual computers these days.

Craig Newman
Craig, I think you don't need to aproach the 100MB file to find yourself waiting too much time for the program to finits her duty when it comes to the situation where you need to consult many big files many times. :shock:

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9645
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Fastest way to know the contents of the latest line of a

Post by dunbarx » Wed Apr 26, 2017 10:24 pm

Hmmm.

Please explain what you are doing. I work almost never with external files.

In LC. I have a 3,000,000 line field with 150 MB of text. I can get any line in 10 ticks. If I put that dataset into an external file, and ask for the millionth line, it takes about the same time. What are you doing?

Craig

Oh yes. 8)

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9823
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Fastest way to know the contents of the latest line of a

Post by FourthWorld » Wed Apr 26, 2017 11:40 pm

dunbarx wrote:In LC. I have a 3,000,000 line field with 150 MB of text. I can get any line in 10 ticks. If I put that dataset into an external file, and ask for the millionth line, it takes about the same time.
Does it? 150 MBs is a big malloc. It would be interesting to know the load time separate from the seek time.

@Daniel: if you have a fairly confident sense of the max length of each line, you could use the seek command to seek to a location within the file that's safely far enough away from the file length to be sure to get a complete line, read at that point until EOF, and then get the last line of the resulting buffer in "it".

In fact, as I think about this you may not even need the extra "seek" statement, since IIRC the "read" command has as optional "at" param that can be used to set a starting offset within the file.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

capellan
Posts: 654
Joined: Wed Aug 15, 2007 11:09 pm

Re: Fastest way to know the contents of the latest line of a

Post by capellan » Thu Apr 27, 2017 12:00 am

Once upon a time, I copied in a 4.7 Gb DVD the full decompressed text of Wikipedia (in spanish)
with the purpose of creating an Offline Wikipedia Reader using LiveCode.

After some failed tests and multiple experiments, I ended up splitting the big file in hundreds
of small text files. Each small text file was indexed and compressed. Accessing data with this method
resulted much, much faster than retrieving data from a single big file.

Al

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”