Contents

Roo

File.each_line()

Invokes the passed function for each line within this File object’s file, passing to the function the line as the first parameter and the line number (1-based) as the second argument. The method can take an optional second parameter as an Array object (arguments). The elements of this array will be passed to the function as additional parameters. Returns the current position of the pointer in the stream as a Number.

file.each_line(func as Function, arguments? as Array) as Number

Parameters

  • func. A reference to a function. This function must have been defined with at least two parameters as the current line will be passed to it first and the line number (one-based) will be passed as the second parameter. This function may have additional parameters defined and these will be passed to it as the contents of the optional arguments array
  • arguments. Optional Array object. Each element of this array will be passed to func as additional parameters after the current line and line number

Returns

Returns the position of the pointer in the stream (file.pos)

Notes

The file must be encoded as UTF-8 or else unexpected things will happen.

Example

function itemise(line, number) {
    print(number + ". " + line)
}

function suffix(line, number, what) {
    print(line + what)
}

# Assume file contents are:
# Iron Man
# Hulk
# Thor

var f = File("/Users/garry/Desktop/test.txt")
f.each_line(itemise)
# Prints:
# 1. Iron Man
# 2. Hulk
# 3. Thor

f.each_line(suffix, ["!"])
# Prints:
# Iron Man!
# Hulk!
# Thor!