Difference between revisions of "Io.lines"

From GiderosMobile
(mostly formatting :-))
Line 1: Line 1:
 
__NOTOC__
 
__NOTOC__
<languages />
+
'''Available since:''' Gideros 2011.6<br/>
'''<translate>Available since</translate>:''' Gideros 2011.6<br/>
+
'''Class:''' [[io]]<br/>
'''<translate>Class</translate>:''' [[Special:MyLanguage/io|io]]<br/>
 
=== <translate>Description</translate> ===
 
<translate>Opens the given file name in read mode and returns an iterator function that, each time it is called, returns a new line from the file. Therefore, the construction
 
  
    ''for line in io.lines(filename) do body end''
+
=== Description ===
 +
Opens the given file name in read mode and returns an iterator function. Each time the iterator function is called, it returns a new line from the file.
 +
<source lang="lua">
 +
(function) = io.lines(filename)
 +
</source>
  
will iterate over all lines of the file. When the iterator function detects the end of file, it returns nil (to finish the loop) and automatically closes the file.
 
  
 +
The construction
 +
<source lang="lua">
 +
for line in io.lines(filename) do body end
 +
</source>
 +
will iterate over all lines of the file. When the iterator function detects the end of file, it returns nil (to finish the loop) and automatically closes the file.
  
The call io.lines() (with no file name) is equivalent to io.input():lines(); that is, it iterates over the lines of the default input file. In this case it does not close the file when the loop ends.</translate>
+
The call '''io.lines()''' (with no file name) is equivalent to '''io.input():lines()''' that is, it iterates over the lines of the default input file. In this case it does not close the file when the loop ends.
 +
 
 +
=== Parameters ===
 +
'''filename''': (string) filename to open '''optional'''<br/>
 +
 
 +
=== Return values ===
 +
'''Returns''' (function) iterator function<br/>
 +
 
 +
=== Example ===
 +
'''Iterates over all lines of a file'''
 
<source lang="lua">
 
<source lang="lua">
(function) = io.lines(filename)
+
for line in io.lines(filename) do
 +
-- do stuff
 +
end
 
</source>
 
</source>
=== <translate>Parameters</translate> ===
 
'''filename''': (string) <translate>filename to open</translate> '''optional'''<br/>
 
=== <translate>Return values</translate> ===
 
'''<translate>Returns</translate>''' (function) <translate>iterator function</translate><br/>
 
  
 
{{Io}}
 
{{Io}}

Revision as of 05:00, 7 December 2020

Available since: Gideros 2011.6
Class: io

Description

Opens the given file name in read mode and returns an iterator function. Each time the iterator function is called, it returns a new line from the file.

(function) = io.lines(filename)


The construction

for line in io.lines(filename) do body end

will iterate over all lines of the file. When the iterator function detects the end of file, it returns nil (to finish the loop) and automatically closes the file.

The call io.lines() (with no file name) is equivalent to io.input():lines() that is, it iterates over the lines of the default input file. In this case it does not close the file when the loop ends.

Parameters

filename: (string) filename to open optional

Return values

Returns (function) iterator function

Example

Iterates over all lines of a file

for line in io.lines(filename) do
	-- do stuff
end