FileReadLine for v2

Post your working scripts, libraries and tools.
User avatar
metacognition
Posts: 117
Joined: 22 Oct 2014, 05:57
Location: Alaska
Contact:

FileReadLine for v2

Post by metacognition » 30 May 2023, 14:05

FileReadLine for v2, in case you don't want to give it up.

Code: Select all

FileReadLine(&output_var, file_to_read, line_number)
{
    file_object := FileOpen(file_to_read, "r")
    loop line_number
    {
        output_var := file_object.ReadLine() 
    }
    file_object.Close
}

User avatar
boiler
Posts: 16768
Joined: 21 Dec 2014, 02:44

Re: FileReadLine for v2

Post by boiler » 31 May 2023, 23:12

I suppose you were going for as close of a match to the v1 version as possible, but if it were carried on to v2, it would have used the return value of the function:

Code: Select all

FileReadLine(file_to_read, line_number)
{
    file_object := FileOpen(file_to_read, "r")
    loop line_number
        line_text := file_object.ReadLine() 
    file_object.Close
    return line_text
}
…so calling it could be used in an expression without needing an output variable:

Code: Select all

MsgBox "This is line 5:`n`n" . FileReadLine("MyFile.txt", 5)

User avatar
metacognition
Posts: 117
Joined: 22 Oct 2014, 05:57
Location: Alaska
Contact:

Re: FileReadLine for v2

Post by metacognition » 01 Jun 2023, 13:35

Bingo! Yes kept same parameters as v1, for easy/lazy script conversion. Heck, we could write it to return the value if the variable reference is missing or unset. But I’m just getting into v2 and feeling nostalgic about the last 15 years of autohotkey helping every job I’ve had. 😀

Post Reply

Return to “Scripts and Functions (v2)”