Copy the Last Line of a .txt file
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller
Copy the Last Line of a .txt file
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
I have tried several things without success.
Tom
-
- VIP Livecode Opensource Backer
- Posts: 9867
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Copy the Last Line of a .txt file
Copy to the clipboard? To the end of the file? For display in the UI?
How big is the file?
How big is the file?
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Copy the Last Line of a .txt file
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
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Copy the Last Line of a .txt file
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
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
-
- VIP Livecode Opensource Backer
- Posts: 9867
- Joined: Sat Apr 08, 2006 7:05 am
- Location: Los Angeles
- Contact:
Re: Copy the Last Line of a .txt file
It's only easy when you already know how.stam wrote: ↑Sat Oct 07, 2023 6:30 amI mean it’s not hard toso one would presume that’s not what you mean…Code: Select all
set the clipboard to line -1 of myTextVariable
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
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn
Re: Copy the Last Line of a .txt file
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
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
Re: Copy the Last Line of a .txt file
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
Re: Copy the Last Line of a .txt file
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.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.
Jim Lambert
-
- VIP Livecode Opensource Backer
- Posts: 9785
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Copy the Last Line of a .txt file
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
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
-
- Livecode Opensource Backer
- Posts: 9476
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Copy the Last Line of a .txt file
"adorable" . . . gies me the boak.the adorable method
- -
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 55 times
Re: Copy the Last Line of a .txt file
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
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.
-
- Livecode Opensource Backer
- Posts: 9476
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Copy the Last Line of a .txt 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.You can read the file directly into a variable that will hold the whole file.
Re: Copy the Last Line of a .txt file
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
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
-
- Livecode Opensource Backer
- Posts: 9476
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Copy the Last Line of a .txt file
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.
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.