ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* CSV parser do not processed macro.
@ 2010-07-31 14:41 Jaroslav Hajtmar
  2010-08-01 18:19 ` Arthur Reutenauer
  0 siblings, 1 reply; 2+ messages in thread
From: Jaroslav Hajtmar @ 2010-07-31 14:41 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Hello all,

to solve my problem I need to find why my function ParseCSVdata 
incorrectly parses strings stored in a macro.
It's including possibly the reason why do not work properly Mojca 
database module as I wrote in a recent mail.
It seems to enter a string function, which is not my function to find a 
separator.

Thanx Jaroslav

Here is my minimal example:


\startluacode
function ParseCSVdata(string2parse, separator, leftdelimiter, 
rightdelimiter)
     -- Funkce pro "rozparsování" jednotlivých záznamů (řádků) CSV tabulky
     -- Vstupní textový řetězec (načtený řádek) je v místech výskytu 
separátoru "rozsekán" na jednotlivá pole
     -- Jediný povinný vstupní parametr je 'string2parse'. Při 
nenastavení zbývajících tří parametrů se jako separátor a oddělovače 
použijí buď globální proměnné Sep, Ld a Rd nebo se použijí defaultní hodnoty
     -- Výstupem je pole oddělených řetězců
     -- Když nejsou globální proměnné Sep, Ld a Rd nastaveny uživatelem, 
nastaví se na defaultní hodnoty. Uživatel si může defaultní hodnoty 
libovolně změnit podle toho, jaké hodnoty používá ve svých aplikacích 
nejčastěji
     Sep = (Sep == nil) and UserCSVSeparator or Sep     -- Pokud není 
glob. neznámá Sep nastavena, použije se defaultní hodnota ';' (středník).
     Ld = (Ld == nil) and UserCSVLeftDelimiter or Ld -- Pokud není glob. 
neznámá Ld (left delimiter) nastavena, použije se defaultní hodnota '' 
(prázdný řetězec)
     Rd = (Rd == nil) and UserCSVRightDelimiter or Rd -- Pokud není 
glob. neznámá Rd (right delimiter) nastavena, použije se defaultní 
hodnota '' (prázdný řetězec)
   -- Nastavení hodnot parametrů, které nejsou při volání funkce 
nastaveny tj. při volání funkce ParseCSVdata(string2parse) s pouze 
jedním parametrem
     local separator = (separator == nil) and Sep or separator -- při 
nenastavení separátoru se použije
     local leftdelimiter = (leftdelimiter == nil) and Ld or leftdelimiter
     local rightdelimiter = (rightdelimiter == nil) and Rd or rightdelimiter
     -- A jdeme na vlastní zpracování řetězce ...
     local result={}
         if leftdelimiter ~= '' and rightdelimiter ~= '' then -- Když 
jsou položky v řádku vymezeny levým i pravým delimiterem (tj. jakousi 
závorkou) např. "pole1";"pole2" nebo {pole1};{pole2} .. atd.
             string.gsub(string2parse, 
leftdelimiter.."(.-)"..rightdelimiter, function(a) 
table.insert(result,a) end ) -- Je vidět, že v tomto případě ani  
nezáleží na separátoru, jednotlivé uzávorkované řetězce se od sebe oddělí
         else -- Když je pouze separátor polí bez vymezujících znaků - 
delimiterů (tento případ bohužel neumožňuje použití separátoru jako 
samostatného znaku v nějakém poli )
           result=string.split(string2parse,separator) -- pak stačí 
vstupní řetezec jen "vysplitovat" tj. rozsekat na samostatné části
         end
     return result  -- Funkce vrátí pole result obsahující oddělená 
jednotlivá pole řádkového CSV záznamu
end

function pars(inppar)
tex.sprint("input parametr (string):  "..inppar.."\\par")
CSVLine=ParseCSVdata(inppar,',','','')
tex.print(table.maxn(CSVLine).." item(s) \\par")
for i = 1, table.maxn(CSVLine) do
tex.sprint(i..". "..CSVLine[i].."\\par")
end
tex.sprint("\\blank[big]")
end

\stopluacode



\def\printaction{x,y,z,u,v}


\starttext

\startluacode
pars("a,b,c,d")
pars("\\printaction")
\stopluacode



\stoptext


___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: CSV parser do not processed macro.
  2010-07-31 14:41 CSV parser do not processed macro Jaroslav Hajtmar
@ 2010-08-01 18:19 ` Arthur Reutenauer
  0 siblings, 0 replies; 2+ messages in thread
From: Arthur Reutenauer @ 2010-08-01 18:19 UTC (permalink / raw)
  To: hajtmar, Mailing list for ConTeXt users

> to solve my problem I need to find why my function ParseCSVdata  
> incorrectly parses strings stored in a macro.

  You can work around the problem by defining a Lua function that calls
the macro:

	\def\parse#1{\directlua{pars("#1")}}

  You can then either call it directly:

	\parse{\printaction}

or from Lua with tex.print:

	\startluacode
	tex.print("\\parse{\\printaction}")
	\stopluacode

and the macros are expanded in the order you want.

  It's admittedly a bit silly, but it works.

  As a side note, the infinitive of the verb related to “parser” is “to
parse” in English, with an ‘e’ at the end.

	Arthur
___________________________________________________________________________________
If your question is of interest to others as well, please add an entry to the Wiki!

maillist : ntg-context@ntg.nl / http://www.ntg.nl/mailman/listinfo/ntg-context
webpage  : http://www.pragma-ade.nl / http://tex.aanhet.net
archive  : http://foundry.supelec.fr/projects/contextrev/
wiki     : http://contextgarden.net
___________________________________________________________________________________

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2010-08-01 18:19 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2010-07-31 14:41 CSV parser do not processed macro Jaroslav Hajtmar
2010-08-01 18:19 ` Arthur Reutenauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).