Module pl.input
Iterators for extracting words or numbers from an input source.
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. |
fields (ids, delim, f, opts) | parse an input source into fields. |
numbers (f) | generate a sequence of numbers from a source. |
words (f) | generate a sequence of words from a source. |
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 text -
pattern
: -
fn
: Optionally can pass a function to process each token as it/s found.
Return value:
- 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)
Return value:
- a getter function
-
- 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 index -
delim
: delimiter to parse fields (default space) -
f
: a source (@see create_getter) -
opts
: option table, {no_fail=true}
Usage:
for x,y in fields {2,3} do print(x,y) end -- 2nd and 3rd fields from stdin
Return value:
- an iterator with the field values
-
- numbers (f)
-
generate a sequence of numbers from a source.
Parameters:
-
f
: A source
Return value:
- An iterator
-
- words (f)
-
generate a sequence of words from a source.
Parameters:
-
f
: A source
Return value:
- An iterator
-