Format.lua: Difference between revisions
No edit summary |
Dubiousjim (talk | contribs) (Category:Lua) |
||
(9 intermediate revisions by 2 users not shown) | |||
Line 10: | Line 10: | ||
'''CODING EXAMPLE:''' | '''CODING EXAMPLE:''' | ||
-- Include/Call for this library | -- Include/Call for this library | ||
require(" | require("format") | ||
-- file test.txt is from dos/windows | -- file test.txt is from dos/windows | ||
require " | require "format" | ||
f = io.open("test.txt") | f = io.open("test.txt") | ||
c = f:read("*a") | c = f:read("*a") | ||
file = | file = format.dostounix(c) | ||
f:close() | |||
--file now contains the contents of test.txt without the \r char | --file now contains the contents of test.txt without the \r char | ||
Line 28: | Line 29: | ||
'''CODING EXAMPLE:''' | '''CODING EXAMPLE:''' | ||
-- Include/Call for this library | -- Include/Call for this library | ||
require(" | require("format") | ||
f = io.open("/etc/shorewall/rules") | |||
c = f:read("*a") | |||
t = format.remove_blanks_comments(c) | |||
f:close() | |||
-- t is a table with only pertinent lines | -- t is a table with only pertinent lines | ||
== table_to_string == | == table_to_string == | ||
Line 78: | Line 49: | ||
'''CODING EXAMPLE:''' | '''CODING EXAMPLE:''' | ||
-- Set variable/Call for this library | -- Set variable/Call for this library | ||
require " | require "format" | ||
-- Create a array of data <span style="color:red">(Not sure if the next row is correct)</span> | -- Create a array of data <span style="color:red">(Not sure if the next row is correct)</span> | ||
arraytojoin = { "Bird", "Fish", "Cow", "Hammer" } | arraytojoin = { "Bird", "Fish", "Cow", "Hammer" } | ||
-- Process the data (note the delimiter) | -- Process the data (note the delimiter) | ||
liboutput = | liboutput = format.table_to_string(";", arraytojoin) | ||
Bird;Fish;Cow;Hammer | Bird;Fish;Cow;Hammer | ||
== string_to_table == | == string_to_table == | ||
NOTE: | |||
for cut functionality do something like <BR> | |||
print(format.string_to_table(" ", "This is a test")[2]) <BR> | |||
gives you the second field which is .... is | |||
'''INPUT:'''<BR> | '''INPUT:'''<BR> | ||
This library required the following inputs/parameters. | This library required the following inputs/parameters. | ||
Line 98: | Line 74: | ||
'''CODING EXAMPLE:''' | '''CODING EXAMPLE:''' | ||
-- Set variable/Call for this library | -- Set variable/Call for this library | ||
require " | require "format" | ||
-- Grab the line from a file or input it into the funtcion | -- Grab the line from a file or input it into the funtcion | ||
line = "bobo~foo~bar~baz~1" | line = "bobo~foo~bar~baz~1" | ||
-- Process the data (note the delimiter) | -- Process the data (note the delimiter) | ||
t = | t = format.string_to_table("~", line) | ||
't' would contain: | 't' would contain: | ||
{ 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1} | { 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1} | ||
Line 111: | Line 87: | ||
4 baz | 4 baz | ||
5 1 | 5 1 | ||
== cap_begin_word == | |||
'''INPUT:'''<BR> | |||
This library required the following inputs/parameters. | |||
* String | |||
'''OUTPUT:'''<BR> | |||
This library deliverers the following output/parameters. | |||
* String with all first letters of words capitalized | |||
'''CODING EXAMPLE:''' | |||
-- Set variable/Call for this library | |||
require "format" | |||
-- Grab the line from a file or input it into the function | |||
line = "The rain in spain stays mainly in the plain" | |||
-- Process the data (note the delimiter) | |||
print(format.cap_begin_word(line)) | |||
--The Rain In Spain Stays Mainly In The Plain | |||
== search_replace == | |||
'''INPUT:'''<BR> | |||
This library required the following inputs/parameters. | |||
* File path or Table | |||
'''OUTPUT:'''<BR> | |||
This library deliverers the following output/parameters. | |||
* Table | |||
'''CODING EXAMPLE:''' | |||
-- Set variable/Call for this library | |||
require "format" | |||
t = { "The man ran home", "See the man walk", "The man laughed"} | |||
print(format.search_replace(t, "[Th]he man", "The boy")[1]) | |||
--The boy ran home | |||
== search_for_lines == | |||
'''INPUT:'''<BR> | |||
This library required the following inputs/parameters. | |||
* File path or table | |||
'''OUTPUT:'''<BR> | |||
This library deliverers the following output/parameters. | |||
* Table | |||
** THIS WILL SEARCH FOR THE WHOLE LINE CONTAINING THE MATCH | |||
'''CODING EXAMPLE:''' | |||
-- Set variable/Call for this library | |||
--say have file like /etc/conf.d/cron with CRON_OPTS="" or more will find them | |||
require "format" | |||
for a,b in ipairs(format.search_for_lines("/etc/conf.d/cron", "OPTS")) do print(a,b) end | |||
== Expand Bash Syntax Vars == | |||
Expand a string with bash-like variables with lua variables | |||
'''INPUT:'''<BR> | |||
This library requires the following inputs/parameters. | |||
* String | |||
'''OUTPUT:'''<BR> | |||
This library deliverers the following output/parameters. | |||
* String | |||
'''CODING EXAMPLE:''' | |||
animal = { cows=3, dogs=2 } | |||
print (expand_bash_syntax_vars ("You have ${animal.dogs} dogs")) | |||
You have 2 dogs | |||
[[Category:Lua]] |
Latest revision as of 08:03, 12 March 2012
dostounix
INPUT:
This library function required the following inputs/parameters.
- file contents
- Example: c = file:read("*a")
- String with EOL
OUTPUT:
This library function deliverers the following output/parameters.
- Output is returned minus the \r characters
CODING EXAMPLE:
-- Include/Call for this library require("format") -- file test.txt is from dos/windows require "format" f = io.open("test.txt") c = f:read("*a") file = format.dostounix(c) f:close() --file now contains the contents of test.txt without the \r char
remove_blanks_comments
INPUT:
This library function required the following inputs/parameters.
- Path
- Example: /root/mynotes
OUTPUT:
This library function deliverers the following output/parameters.
- Returns a table to iterate over minus the blank and commented lines
CODING EXAMPLE:
-- Include/Call for this library require("format") f = io.open("/etc/shorewall/rules") c = f:read("*a") t = format.remove_blanks_comments(c) f:close() -- t is a table with only pertinent lines
table_to_string
INPUT:
This library required the following inputs/parameters.
- Delimiter
- Could be one or more chars.
- Array
- Data which is to be joined into a string.
OUTPUT:
This library deliverers the following output/parameters.
- String
- Could be something like "Word, Word, Word, Word"
CODING EXAMPLE:
-- Set variable/Call for this library
require "format"
-- Create a array of data (Not sure if the next row is correct)
arraytojoin = { "Bird", "Fish", "Cow", "Hammer" }
-- Process the data (note the delimiter)
liboutput = format.table_to_string(";", arraytojoin)
Bird;Fish;Cow;Hammer
string_to_table
NOTE:
for cut functionality do something like
print(format.string_to_table(" ", "This is a test")[2])
gives you the second field which is .... is
INPUT:
This library required the following inputs/parameters.
- Delimiter
- Could be one or more chars.
- String or line from a file: bobo~foo~bar~baz~1
- Data is to be split into a table
OUTPUT:
This library deliverers the following output/parameters.
- Table
- { 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1}
CODING EXAMPLE:
-- Set variable/Call for this library require "format" -- Grab the line from a file or input it into the funtcion line = "bobo~foo~bar~baz~1" -- Process the data (note the delimiter) t = format.string_to_table("~", line) 't' would contain: { 1 = "bobo", 2 = "foo", 3 = "bar", 4 = "baz", 5 = 1} for a,b in ipairs(t) do print(a,b) end 1 bobo 2 foo 3 bar 4 baz 5 1
cap_begin_word
INPUT:
This library required the following inputs/parameters.
- String
OUTPUT:
This library deliverers the following output/parameters.
- String with all first letters of words capitalized
CODING EXAMPLE:
-- Set variable/Call for this library require "format" -- Grab the line from a file or input it into the function line = "The rain in spain stays mainly in the plain" -- Process the data (note the delimiter) print(format.cap_begin_word(line)) --The Rain In Spain Stays Mainly In The Plain
search_replace
INPUT:
This library required the following inputs/parameters.
- File path or Table
OUTPUT:
This library deliverers the following output/parameters.
- Table
CODING EXAMPLE:
-- Set variable/Call for this library require "format" t = { "The man ran home", "See the man walk", "The man laughed"} print(format.search_replace(t, "[Th]he man", "The boy")[1]) --The boy ran home
search_for_lines
INPUT:
This library required the following inputs/parameters.
- File path or table
OUTPUT:
This library deliverers the following output/parameters.
- Table
- THIS WILL SEARCH FOR THE WHOLE LINE CONTAINING THE MATCH
CODING EXAMPLE:
-- Set variable/Call for this library --say have file like /etc/conf.d/cron with CRON_OPTS="" or more will find them require "format" for a,b in ipairs(format.search_for_lines("/etc/conf.d/cron", "OPTS")) do print(a,b) end
Expand Bash Syntax Vars
Expand a string with bash-like variables with lua variables
INPUT:
This library requires the following inputs/parameters.
- String
OUTPUT:
This library deliverers the following output/parameters.
- String
CODING EXAMPLE:
animal = { cows=3, dogs=2 } print (expand_bash_syntax_vars ("You have ${animal.dogs} dogs")) You have 2 dogs