Answering Questions from elsewhere #1: Reading and writing text to / from a file

All flavors welcome.
Forum rules
Be kind.
Post Reply
User avatar
richmond62
Posts: 2769
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by richmond62 »

I have a file with 50,000 rows of about 25 chars per row. I only need to read roughly the most recent 200 rows until something is true and it saves time not to load the entire file using put ULR "file:filename" into tWhatever

I can append data to the file easily (a line at a time) but then I'd have to read the file backwards - which I can't see how to do.
OR
I can prepend data to the file then read in the normal order, but I can seem to do that without it destroys the previous data.

It's odd I can't find a solution as I'm sure this is a problem that has been faced by so many people so many times, that there is an elegant solution that I am missing. In short, I DO NOT want to load the entire file into memory using the URL command. I just want to read the 200 most recently contributed lines for the purpose of time efficiency - in this case, speed matters.
I have alerted the OP, in a private message, that I have answered their question over here.
https://richmondmathewson.owlstown.net/
User avatar
richmond62
Posts: 2769
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by richmond62 »

This is extremely easy . . .

I knocked together a very simple stack:
-
SShot 2024-03-08 at 11.07.21.png
SShot 2024-03-08 at 11.07.21.png (73.36 KiB) Viewed 362 times
-
The button "GET'EM" contains this code:

Code: Select all

on mouseUp
   put empty into fld "fINPUT"
   put 1 into LYNE
   repeat until LYNE > 200
      put line LYNE of URL "file:///Users/richmond/Desktop/Hax 8 March/AIW.txt" & " " after fld "fINPUT"
      add 1 to LYNE
      end repeat
end mouseUp
Obviously the URL is 'peculiar' to my machine and the document I am using for this test.
https://richmondmathewson.owlstown.net/
xAction
Posts: 285
Joined: Thu Sep 16, 2021 1:40 pm
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by xAction »

isn't it just this?
on mouseUp
answer file "Select a Huge File Greater than 200 Lines"
if it is empty then exit mouseUp

put "file:" & it into tOriginalFilePath
put line 1 to 200 of URL tOriginalFilePath into LinesToChange

--- changing case of first 200 lines as a test
--- and shoving them right back where we got them
put toUpper(LinesToChange) into line 1 to 200 of URL tOriginalFilePath

end mouseUp
if the number of lines changed then you could do
put number of lines of LinesToChange into tLinesChangedOffset
put toUpper(LinesToChange ) &cr& line tLinesChangedOffset to -1 of URL tOriginalFilePath into URL tOriginalFilePath
and if you don't want to lose file in event of ...issues and assuming the file has standard 3 char extension
put char 1 to -5 of tOriginalFilePath &"_BackUp" & char 4 to -1 of tOriginalFilePath into BackUpPath
revcopyFIle tOriginalFilePath to BackUpPath
User avatar
richmond62
Posts: 2769
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by richmond62 »

isn't it just this?
Not completely, as the OP wants to be able to read the first 200 lines.

Mind you:
I only need to read roughly the most recent 200 rows until something is true
Could mean they want to load lines 1 - 200 into a field.

HOWEVER, it could mean that they just want to do a spot of pattern matching.
https://richmondmathewson.owlstown.net/
User avatar
richmond62
Posts: 2769
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by richmond62 »

At this point:
-
SShot 2024-03-08 at 12.53.37.png
SShot 2024-03-08 at 12.53.37.png (38.32 KiB) Viewed 343 times
-

Code: Select all

on mouseUp
   put empty into fld "fRESULT"
   put 1 into LYNE
   repeat until LYNE > 200
      put line LYNE of URL "file:///Users/richmond/Desktop/Hax 8 March/AIW.txt" into XYZ
      if XYZ contains "MARMALADE" then
         put "line" && LYNE && "contains 'MARMALADE'." into fld "fRESULT"
         add 400 to LYNE
      end if
      add 1 to LYNE
   end repeat
end mouseUp
This script bottles out after the first appearance of the search term.
https://richmondmathewson.owlstown.net/
FourthWorld
Posts: 280
Joined: Sat Sep 11, 2021 4:37 pm
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by FourthWorld »

Every time a URL reference is obtained the entire contents of the file are read from disk.

Klaus reminded the readers "over there" of that.

In your example, you should see a noticeable speed boost by reading the file only once, and then iterating its contents from a variable.

Side question: how is this conversation of such specific relevance to OXT to merit splitting the thread into a separate subthread here?
User avatar
richmond62
Posts: 2769
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by richmond62 »

how is this conversation of such specific relevance to OXT to merit splitting the thread into a separate subthread here?
It isn't: you are missing the point:

viewtopic.php?t=957

viewtopic.php?t=964
https://richmondmathewson.owlstown.net/
User avatar
richmond62
Posts: 2769
Joined: Sun Sep 12, 2021 11:03 am
Location: Bulgaria
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by richmond62 »

Later on it became obvious the OP wanted to read from the end of their list, not the start . . .
https://richmondmathewson.owlstown.net/
FourthWorld
Posts: 280
Joined: Sat Sep 11, 2021 4:37 pm
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by FourthWorld »

richmond62 wrote: Sat Mar 09, 2024 10:53 am
how is this conversation of such specific relevance to OXT to merit splitting the thread into a separate subthread here?
It isn't: you are missing the point:

viewtopic.php?t=957

viewtopic.php?t=964
Agreed: I've read both of those, and I still don't see the point.

Nevermind. Carry on...
User avatar
OpenXTalkPaul
Posts: 1574
Joined: Sat Sep 11, 2021 4:19 pm
Contact:

Re: Answering Questions from elsewhere #1: Reading and writing text to / from a file

Post by OpenXTalkPaul »

Does using a URL, as opposed to some other data container, read the whole file from disk/net into memory?
I'm not sure that it does, unless you pass it to another handler (without using the '@', pass by reference symbol).
Post Reply

Who is online

Users browsing this forum: No registered users and 18 guests