lua-users home
lua-l archive

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


> -----Original Message-----
> From: lua-l-bounces@lists.lua.org [mailto:lua-l-bounces@lists.lua.org] On
> Behalf Of meino.cramer@gmx.de
> Sent: woensdag 12 maart 2014 19:49
> To: Lua mailing list
> Subject: Problems with: Most efficient way to recognize a line by its first
> three characters
> 
> Hi,
> 
> I decided to start a new thread about this, since I think
> I stumbled accross a problem, which not really has something
> to do with the "Most efficient way..." as such.
> 
> I asked, whether it is ok to make the format of the data
> public and it is ok -- it was already public (which I didnt know ;)
> 
> I am trying to check all the nice suggestion which were
> given in the originating thread (THANK YOU!) and started
> with this code:
> 
> 
> #! /usr/bin/lua
> 
> function scan ()
>  local bad=function (prefix,info) print("Not recognized"..prefix) end
>  local handlers={
>   ["$1;"]=function( prefix, info ) print ( "$1;")  end,
>   ["$2;"]=function( prefix, info ) print ( "$2;")  end,
>   ["$3;"]=function( prefix, info ) print ( "$3;")  end,
>   ["$4;"]=function( prefix, info ) print ( "$4;")  end,
>   ["$5;"]=function( prefix, info ) print ( "$5;")  end,
>   ["$6;"]=function( prefix, info ) print ( "$6;")  end,
>   ["§7;"]=function( prefix, info ) print ( "§7;")  end,
>   ["§8;"]=function( prefix, info ) print ( "§8;")  end,
>   ["CFG"]=function( prefix, info ) print ( "CFG")  end,
>   ["DAT"]=function( prefix, info ) print ( "DAT")  end,
>   ["KEY"]=function( prefix, info ) print ( "KEY")  end,
>   ["REF"]=function( prefix, info ) print ( "REF")  end,
>   ["VER"]=function( prefix, info ) print ( "VER")  end
>  }
>  local f = assert(io.open("/tmp/example.txt","r") )
>  local line
> 
>  while true do
>    line = f:read("*line" )
>    if not line then break end
>  	local prefix, info = line:sub(1,3), line:sub(4)
>  	local h = handlers[prefix] or bad
>  	h(prefix,info)
>  end
> end
> 
> scan()
> 
> 
> Everything works fine...except the recongition of the lines
> beginning with '§'...

What encoding do you use? Remember that `sub(1,3)` returns the first three _bytes_, which are not necessarily 3 characters.


> And I have not a single idea, why...
> 
> Thank you very mcuh in advance for any help!
> Best regards,
> mcc
> 
> 
>