Module pl.input
Iterators for extracting words or numbers from an input source.
require 'pl'
local total,n = seq.sum(input.numbers())
print('average',total/n)
See here
Dependencies: pl.utils
Functions
alltokens (getter, pattern, fn) | create an iterator over all tokens. |
create_getter (f) | create a function which grabs the next value from a source. |
numbers (f) | generate a sequence of numbers from a source. |
words (f) | generate a sequence of words from a source. |
fields (ids, delim, f, opts) | parse an input source into fields. |
Functions
- alltokens (getter, pattern, fn)
-
create an iterator over all tokens.
based on allwords from PiL, 7.1
Parameters:
getter
: any function that returns a line of textpattern
:fn
: Optionally can pass a function to process each token as it/s found.
Returns:
-
an iterator
- create_getter (f)
-
create a function which grabs the next value from a source. If the source is a string, then the getter
will return the string and thereafter return nil. If not specified then the source is assumed to be stdin.
Parameters:
f
: a string or a file-like object (i.e. has a read() method which returns the next line)
Returns:
-
a getter function
- numbers (f)
-
generate a sequence of numbers from a source.
Parameters:
f
: A source
Returns:
-
An iterator
- words (f)
-
generate a sequence of words from a source.
Parameters:
f
: A source
Returns:
-
An iterator
- fields (ids, delim, f, opts)
-
parse an input source into fields.
By default, will fail if it cannot convert a field to a number.
Parameters:
ids
: a list of field indices, or a maximum field indexdelim
: delimiter to parse fields (default space)f
: a source @see create_getteropts
: option table, {no_fail=true}
Usage:
for x,y in fields {2,3} do print(x,y) end -- 2nd and 3rd fields from stdin
Returns:
-
an iterator with the field values