Copy the Last Line of a .txt 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
trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Copy the Last Line of a .txt file

Post by trags3 » Sat Oct 07, 2023 12:30 am

I want to be able to copythe last line in a txt file. The file can get very long so I don't want to scroll through the entire file.
I have tried several things without success.
Tom

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

Re: Copy the Last Line of a .txt file

Post by FourthWorld » Sat Oct 07, 2023 5:27 am

Copy to the clipboard? To the end of the file? For display in the UI?

How big is the file?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Copy the Last Line of a .txt file

Post by stam » Sat Oct 07, 2023 6:30 am

trags3 wrote:
Sat Oct 07, 2023 12:30 am
I want to be able to copythe last line in a txt file. The file can get very long so I don't want to scroll through the entire file.
I have tried several things without success.
Tom
Perhaps post what you’ve tried and people may be able to help?
I mean it’s not hard to

Code: Select all

set the clipboard to line -1 of myTextVariable
so one would presume that’s not what you mean…

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

Re: Copy the Last Line of a .txt file

Post by dunbarx » Sat Oct 07, 2023 5:11 pm

Stam.

The OP's LC subject history is at a "higher" level, than, say, mine, which is down in the mud. I bet that he did not know one could directly access the last line in a body of text in the way you pointed out.

I may be wrong. If so then like you I wonder if we are grokking his issue.

Tom?

Craig

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

Re: Copy the Last Line of a .txt file

Post by FourthWorld » Sat Oct 07, 2023 5:33 pm

stam wrote:
Sat Oct 07, 2023 6:30 am
I mean it’s not hard to

Code: Select all

set the clipboard to line -1 of myTextVariable
so one would presume that’s not what you mean…
It's only easy when you already know how.

Being able to use negative numbers to count backwards from the implied end of a string of powerful, but not obvious.

And he'd still need to get the file contents onto the variable.

While pop culture most commonly refers to Dunning-Kruger Syndrome as a way of making fun of people who presume expertise without knowing what constitutes expertise, the study was a serious work and has a second half in the outcomes: some who have expertise forget how hard it was for them to acquire it, and thus underestimate the difficulty for newcomers in acquiring it.

I'm guilty of that myself now and then.

Many threads here are filled with guessing replies and little guidance from the OP.

Perhaps we can let trags3 answer the questions I posted to better understand the details of what's needed and how earlier attempts didn't work out.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

trags3
Posts: 418
Joined: Wed Apr 09, 2014 1:58 am
Location: Las Vegas, NV

Re: Copy the Last Line of a .txt file

Post by trags3 » Sat Oct 07, 2023 6:00 pm

Hi Sorry I wasn't clear re what I am trying to do.
I am working on an APP that will track when different products are built & when distributed distributed to multiple customers. These (edible) products are made up with items that have Lot Numbers that the FDA requires be rigorously tracked. The current process uses pen and paper to record all the required data.
Some of the items going into product are built with the same lot number for as long as several months.
I have a card that allows the user to select any of a number of items and fill out the Lot Number , Date, Quantity etc when the product is packaged and then another card when that product is combined with other products and delivered to a customer (retail).
At each step I save the data in a CSV text file (tLine5.txt) one line per record. There are 8 items per line. I want the data from the previous build to populate the card with items that don't change frequently such as the lot number.
To accomplish this I want to read the last line in the appropriate file and populate the card with the data.
I don't want to scroll thru the entire file to get to the last line if possible.
I can't figure out how to get to the last line in the file without starting at the first and scrolling until I get to the EOF.
Thank You for your help!
Tom

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Copy the Last Line of a .txt file

Post by jiml » Sat Oct 07, 2023 6:10 pm

I don't want to scroll thru the entire file to get to the last line if possible.
I can't figure out how to get to the last line in the file without starting at the first and scrolling until I get to the EOF.

Code: Select all

function getLastline myFilePath
   open  file myFilePath for read
   -- read a chunk from the end of the file that's large enough to include the file's last line 
   read from file myFilePath at -1000 for 1000 
   close file (fld cFile)
   return line -1 of it
end getLastline

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Copy the Last Line of a .txt file

Post by jiml » Sat Oct 07, 2023 6:24 pm

At each step I save the data in a CSV text file (tLine5.txt) one line per record.
...
To accomplish this I want to read the last line in the appropriate file and populate the card with the data.
I don't want to scroll thru the entire file to get to the last line if possible.
Alternately, You might consider reversing the line order when you create the CSV file so that the last line is first. In that way you only need to READ line 1 to get the data you need.

Jim Lambert

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

Re: Copy the Last Line of a .txt file

Post by dunbarx » Sat Oct 07, 2023 7:08 pm

Tom.

Whatever the mechanics of reading your file, do you see the adorable method of securing the last line in a dataSet of lines? You instantly see that the first line is logical and straightforward. With a little mental practice, so are the lines at the end.

This is a great tool to know. To get the third to last line, use "line - 3 of..." It is simply the inverse of what is more commonly used, "line 3 of..." Our brains just need a bit of regrooving.

Of course, any chunk can work the same way, characters, words, items, paragraphs...

One cannot do this with controls, as opposed to chunks. Though one can "select btn 3", one cannot "select btn -3". But since LC firstly interprets this sort of thing as layer order, I do not really see why not.

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9446
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Copy the Last Line of a .txt file

Post by richmond62 » Sat Oct 07, 2023 8:36 pm

the adorable method
"adorable" . . . gies me the boak.
-
Screen Shot 2023-10-07 at 22.32.13.png
-

Code: Select all

put fld "rezult"  into REZULT
   put the last line of REZULT into fld "lastLine"
Attachments
Leg Chopper.livecode.zip
Stack.
(9.05 KiB) Downloaded 52 times

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Copy the Last Line of a .txt file

Post by stam » Sat Oct 07, 2023 8:48 pm

In an attempt to clarify and simply:
You don't need to read the file line by line as you may do with other languages, neither is any 'scrolling' of lines and checking for EOF required.
You can read the file directly into a variable that will hold the whole file.

You can access the last line by either using:
line -1 - the negative reverses the direction, so -1 is the last line, not the first, very handy for any kind of container/list where item -1 is the last item/line/char etc. Not many (if any) other languages have this convenience.
the last line - self explanatory

While you could do it other ways, this is plenty fast and will likely save you headaches.

in other words you could create a function

Code: Select all

function getLastLineOfFile pFile        #  pFile is the file path to your text file
   local tText 
   put URL ("file:" & pFile) into tText
   return line -1 of tText  -- or alternatively: return the last line of tText -- it's exactly the same
end function getLastLineOfFile
Last edited by stam on Sat Oct 07, 2023 8:50 pm, edited 1 time in total.

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9446
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Copy the Last Line of a .txt file

Post by richmond62 » Sat Oct 07, 2023 8:50 pm

You can read the file directly into a variable that will hold the whole file.
Of course you can, and it is a whole lot more sensible than bunging the text in a field: the reason I did that in my stack was just for illustrative purposes, so that one can check that one HAS the last line.

stam
Posts: 2741
Joined: Sun Jun 04, 2006 9:39 pm
Location: London, UK

Re: Copy the Last Line of a .txt file

Post by stam » Sat Oct 07, 2023 8:53 pm

Richmond I wasn't referencing your answer - your answer is correct.

I'm referencing the fact that in other languages you would open a text file as a text input stream and either readAll or readLine until EOF (end of file).
It's worth learning about other languages if only to see how good we have it in LC ;)

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9446
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Copy the Last Line of a .txt file

Post by richmond62 » Sat Oct 07, 2023 9:09 pm

I was reading in text files with PASCAL V in 1984, so am well aware of the tedium.

My computing project at Durham was to make a concordance of the English translation of Leibniz's Monodology with multi-dimensional arrays. It brought the University computer to a stand-still.

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”