Version: v0.7.1 - Beta.  We welcome contributors & feedback.

File.readLines

File.readLines($filePath, $lineFunction) -> any

Description

Read a file one line at a time.

The $filePath is relative to the data/files directory.

For each line, call $lineFunction, which receives the line (without a newline character) as its only argument.

// Count the number of inspirational quotes
$numProgrammingQuotes = 0

File.readLines('quotes.txt', fn ($line) {

    if $line.contains('programming', -ignoreCase) {
        $numProgrammingQuotes += 1
    }
})

Early Return

If $lineFunction returns a non-false value, it will stop reading the rest of the file and return that value.

If all lines are read without halting, it will return false.

// Find the line that has "THT"
$thtLine = File.readLines('book.txt', fn ($line) {

    if $line.contains('THT') {
        return $line
    }

})

print($thtLine)
//= e.g. 'THT makes my hair look full and radiant!'

See Also