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)
source is defined as a string or a file-like object (i.e. has a read() method which returns the next line)
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 function any function that returns a line of text
- pattern string
- fn string Optionally can pass a function to process each token as it's found. (optional)
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 index
- delim string delimiter to parse fields (default space)
- f a source @see create_getter
- opts
table
option table,
{no_fail=true}
Returns:
-
an iterator with the field values
Usage:
for x,y in fields {2,3} do print(x,y) end -- 2nd and 3rd fields from stdin