lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


Hello Alessandro Delgado and Kevin Martin,
Many thanks for your help.

Kevin, your code worked perfectly.
The only change I did, was the replacement the line error(string.format("Unable to parse: '%s'", 
line)) with break.
Because with last line of file, the error is generated.

Again, many thanks.

Best regards,

Ranier Vilela 

------- Original Message -------
>From    : Kevin Martin[mailto:kev82@khn.org.uk]
Sent    : 18/12/2012 11:14:19
To      : lua-l@lists.lua.org
Cc      : 
Subject : RE: Re: Lua pattern

 
On 18 Dec 2012, at 12:55, ranier@cultura.com.br wrote:

> Anybody can help with pattern to process  http://www.bcb.gov.br/rex/ftp/paises.txt 

The file is fixed width, so the second field has a lot of spaces I assume you don't want to capture?

The below works on Lua 5.1. On 5.2 you can replace the %a%d%p with %g

Thanks,
Kev

local parsed = {}
for line in io.lines() do
	local num, val = line:match("^(%d+) ([%a%d%p ]+[%a%d%p])%s+$")
	if num == nil then
		error(string.format("Unable to parse: '%s'", line))
	end

	local t = {}
	t.num = num
	t.val = val

	parsed[#parsed+1] = t
end

for i, t in ipairs(parsed) do
	print(string.format("%d: '%s' '%s'", i, t.num, t.val))
end