SoFunction
Updated on 2025-04-07

Lua implements file reading methods in positive and reverse order

Lua implements file reading methods in positive and reverse order

Updated: April 22, 2015 10:28:09 Submission: junjie
This article mainly introduces Lua's file reading method to implement positive and reverse order. This article explains the use of table to generate linked lists to complete the file reading function of positive and reverse order. Friends who need it can refer to it
--table characteristic
-- usetableGenerate a list of positive and reverse orders

-- usetableGenerate a link table

list = nil
local file = ("","r") -->Open this book

pre = nil
--Read this file in line orderlistmiddle
for line in file:lines() do  
	current = {next = nil,value = line}
	pre = pre or current
	list = list or pre
	 = current
	pre = current
end

file:close() -- Close the file

-- Outputlist
local l = list
while l do
	print()
	l = 
end

-- The following is the method of reverse order by row
print("The following is the output file in reverse order:\n")
local file = ("","r") -->Open this book

list = nil --ClearlistPrevious content

for line in file:lines() do
	list = {next = list,value = line}
end

file:close() -- Close the file
-- Outputlist
local l = list
while l do
	print()
	l = 
end
  • Lua
  • Formal order
  • Inverse order
  • File reading

Related Articles

  • Meta-methods in Lua__newindex Detailed explanation

    This article mainly introduces the detailed explanation of the meta method in Lua__newindex. This article explains the contents of query and update, monitoring assignment, assignment of values ​​to another table through table. Friends who need it can refer to it.
    2014-09-09
  • Lua returns a Closures function instance

    This article mainly introduces Lua to return a Closures function instance. This article directly gives code examples. Friends who need it can refer to it.
    2015-04-04
  • Lua's function environment and package example explanation

    This article mainly introduces Lua's function environment and package example explanation. This article decomposes the definition, implementation method and usage method of the function environment and package. Friends who need it can refer to it.
    2014-09-09
  • Write Lua extension so file and call method examples under Linux

    This article mainly introduces the writing of Lua extension so file and call method examples under Linux. This article gives C language code, compile SO file, and call code examples in Lua. Friends who need it can refer to it
    2015-05-05
  • Lua script implements recursive deletion of a folder

    This article mainly introduces the implementation of Lua script to recursively delete a folder. This article gives the implementation code of C++ and Lua versions. Friends who need it can refer to it.
    2015-05-05
  • Introduction and advantages and disadvantages of Lua coroutine

    Today, the editor will share with you an introduction and advantages and disadvantages of the Lua collaborative program coroutine. The editor thinks the content is quite good. Now I share it with you. It has good reference value. Friends in need will follow the editor to take a look.
    2018-10-10
  • Meta tables and meta method learning notes in Lua

    This article mainly introduces the meta tables and meta method learning notes in Lua. This article mainly explains the contents of getting the meta table, setting the meta table modification, etc. If you need it, please refer to it.
    2014-12-12
  • Calling Lua and source code decomposition in Cocos2d-x

    This article mainly introduces the call to Lua and source code decomposition in Cocos2d-x. The article finally summarizes some Lua syntax. Friends who need it can refer to it.
    2014-09-09
  • Example of meta-method execution of arithmetic class in Lua

    This article mainly introduces examples of using metatables to perform arithmetic meta methods in Lua. This article gives addition, subtraction, multiplication, division, opposite numbers, modulus, etc., friends who need it can refer to it.
    2014-09-09
  • Summary of iterators and generic for learning in Lua

    This article mainly introduces the iterator and generic for learning summary in Lua. This article explains the basic knowledge of iterators and generic for, the semantics of generic for, stateless iterators, etc. You can refer to it.
    2014-09-09

Latest Comments