Page 1 of 1

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

Posted: Fri Mar 08, 2024 8:45 am
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.

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

Posted: Fri Mar 08, 2024 9:07 am
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 1139 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.

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

Posted: Fri Mar 08, 2024 10:17 am
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

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

Posted: Fri Mar 08, 2024 10:40 am
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.

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

Posted: Fri Mar 08, 2024 10:54 am
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 1120 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.

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

Posted: Fri Mar 08, 2024 11:58 pm
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?

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

Posted: Sat Mar 09, 2024 10:53 am
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

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

Posted: Sat Mar 09, 2024 2:25 pm
by richmond62
Later on it became obvious the OP wanted to read from the end of their list, not the start . . .

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

Posted: Mon Mar 11, 2024 12:07 am
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...

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

Posted: Mon Mar 11, 2024 9:57 pm
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).