ntg-context - mailing list for ConTeXt users
 help / color / mirror / Atom feed
* need help comparing strings
@ 2020-10-13 15:33 Pablo Rodriguez
  2020-10-13 15:54 ` Aditya Mahajan
                   ` (2 more replies)
  0 siblings, 3 replies; 8+ messages in thread
From: Pablo Rodriguez @ 2020-10-13 15:33 UTC (permalink / raw)
  To: mailing list for ConTeXt users

Dear list,

I have the following sample:

  \starttext
  \def\numbersfname
    {\cldcontext{string.match([[\env{fname}]], "\letterpercent d+")}}
  \ctxlua{ c = context.getvariable("environment", "fname") or "a" }
  \ctxlua{ d = [[\env{fname}]] }

  tex: \numbersfname\\
  \doifelse{}
      {123}{yes}{no}\\
  c: \cldcontext{c}\\
  \doifelse{\cldcontext{c:match("\letterpercent d+")}}
      {123}{yes}{no}\\
  d: \cldcontext{d}\\
  \doifelse{\cldcontext{d:match("\letterpercent d+")}}
      {123}{yes}{no}
  \stoptext

For compilation, I run the command:

  context --purgeall --arguments=fname={abc123} test-file.tex

These things are failing:

1. I don’t know how to get \numbersfname equal to "123". I don’t know
what I’m doing wrong.

2. Is there no Lua version of \getvariable{...}{...}?

3. I’m trying this on a Linux box, but on Windows I would say that Lua
variable "d" is empty.

I need to get the number from \env{fname} and I don’t know what I’m
missing. \doif is the way to test whether it’s right.

Could anyone explain me what I’m missing here?

Many thanks,

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 15:33 need help comparing strings Pablo Rodriguez
@ 2020-10-13 15:54 ` Aditya Mahajan
  2020-10-13 17:06   ` Pablo Rodriguez
  2020-10-13 16:14 ` Wolfgang Schuster
  2020-10-13 17:16 ` Pablo Rodriguez
  2 siblings, 1 reply; 8+ messages in thread
From: Aditya Mahajan @ 2020-10-13 15:54 UTC (permalink / raw)
  To: mailing list for ConTeXt users

[-- Attachment #1: Type: text/plain, Size: 1709 bytes --]

On Tue, 13 Oct 2020, Pablo Rodriguez wrote:

> Dear list,
> 
> I have the following sample:
> 
>   \starttext
>   \def\numbersfname
>     {\cldcontext{string.match([[\env{fname}]], "\letterpercent d+")}}
>   \ctxlua{ c = context.getvariable("environment", "fname") or "a" }
>   \ctxlua{ d = [[\env{fname}]] }
> 
>   tex: \numbersfname\\
>   \doifelse{}
>       {123}{yes}{no}\\
>   c: \cldcontext{c}\\
>   \doifelse{\cldcontext{c:match("\letterpercent d+")}}
>       {123}{yes}{no}\\
>   d: \cldcontext{d}\\
>   \doifelse{\cldcontext{d:match("\letterpercent d+")}}
>       {123}{yes}{no}
>   \stoptext
> 
> For compilation, I run the command:
> 
>   context --purgeall --arguments=fname={abc123} test-file.tex
> 
> These things are failing:
> 
> 1. I don’t know how to get \numbersfname equal to "123". I don’t know
> what I’m doing wrong.
> 
> 2. Is there no Lua version of \getvariable{...}{...}?
> 
> 3. I’m trying this on a Linux box, but on Windows I would say that Lua
> variable "d" is empty.
> 
> I need to get the number from \env{fname} and I don’t know what I’m
> missing. \doif is the way to test whether it’s right.
> 
> Could anyone explain me what I’m missing here?

I find it simpler to write macros where there is  clean communicating between tex and lua. For example:

    \startluacode
      userdata = userdata or {}
      function userdata.numbersfname(name)
          return string.match(name, "%d+")
      end
    \stopluacode

    \define[1]\numbersfname
        {\cldcontext{userdata.numbersfname([==[#1]==])}}

    \starttext
    \numbersfname{abc123}

    \numbersfname{\env{fname}}
    \stoptext


Aditya

[-- Attachment #2: Type: text/plain, Size: 493 bytes --]

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 15:33 need help comparing strings Pablo Rodriguez
  2020-10-13 15:54 ` Aditya Mahajan
@ 2020-10-13 16:14 ` Wolfgang Schuster
  2020-10-13 17:09   ` Pablo Rodriguez
  2020-10-13 17:16 ` Pablo Rodriguez
  2 siblings, 1 reply; 8+ messages in thread
From: Wolfgang Schuster @ 2020-10-13 16:14 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Pablo Rodriguez

Pablo Rodriguez schrieb am 13.10.2020 um 17:33:
> Dear list,
>
> I have the following sample:
>
>    \starttext
>    \def\numbersfname
>      {\cldcontext{string.match([[\env{fname}]], "\letterpercent d+")}}
>    \ctxlua{ c = context.getvariable("environment", "fname") or "a" }
>    \ctxlua{ d = [[\env{fname}]] }
>
>    tex: \numbersfname\\
>    \doifelse{}
>        {123}{yes}{no}\\
>    c: \cldcontext{c}\\
>    \doifelse{\cldcontext{c:match("\letterpercent d+")}}
>        {123}{yes}{no}\\
>    d: \cldcontext{d}\\
>    \doifelse{\cldcontext{d:match("\letterpercent d+")}}
>        {123}{yes}{no}
>    \stoptext
>
> For compilation, I run the command:
>
>    context --purgeall --arguments=fname={abc123} test-file.tex
>
> These things are failing:
>
> 1. I don’t know how to get \numbersfname equal to "123". I don’t know
> what I’m doing wrong.
>
> 2. Is there no Lua version of \getvariable{...}{...}?

There is no need because everything is stored in a Lua table.


%%%% begin example
\starttext

\startluacode

local argumentlist  = document.arguments.arguments
local argumenttable = utilities.parsers.settings_to_hash(argumentlist)

context(argumenttable["fname"])

\stopluacode

\stoptext
%%%% end example


I tested the example with:

     context --arguments="fname=abc123" <filename>


Wolfgang

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 15:54 ` Aditya Mahajan
@ 2020-10-13 17:06   ` Pablo Rodriguez
  2020-10-13 17:14     ` Wolfgang Schuster
  0 siblings, 1 reply; 8+ messages in thread
From: Pablo Rodriguez @ 2020-10-13 17:06 UTC (permalink / raw)
  To: ntg-context

On 10/13/20 5:54 PM, Aditya Mahajan wrote:
> On Tue, 13 Oct 2020, Pablo Rodriguez wrote:
>> [...]
>> Could anyone explain me what I’m missing here?

Many thanks for your reply, Aditya.

> I find it simpler to write macros where there is clean communicating
> between tex and lua. For example:
> [...]
>     \define[1]\numbersfname
>         {\cldcontext{userdata.numbersfname([==[#1]==])}}

This is the first time I see that way of wrapping an argument.

In any case, I cannot compare to a number:

  \startluacode
    userdata = userdata or {}
    function userdata.numbersfname(name)
        return string.match(name, "%d+")
    end
  \stopluacode

  \define[1]\numbersfname
      {\cldcontext{userdata.numbersfname([==[#1]==])}}

  \starttext
  \numbersfname{abc123}

  \numbersfname{\env{fname}}
  \doifelse{\numbersfname{\env{fname}}}{123}{yes}{no}
  \stoptext

I don’t know why both strings don’t match.

Many thanks for your help,

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 16:14 ` Wolfgang Schuster
@ 2020-10-13 17:09   ` Pablo Rodriguez
  0 siblings, 0 replies; 8+ messages in thread
From: Pablo Rodriguez @ 2020-10-13 17:09 UTC (permalink / raw)
  To: ntg-context

On 10/13/20 6:14 PM, Wolfgang Schuster wrote:
> Pablo Rodriguez schrieb am 13.10.2020 um 17:33:
>> [...]
>> 2. Is there no Lua version of \getvariable{...}{...}?
>
> There is no need because everything is stored in a Lua table.
> [...]
>   local argumentlist  = document.arguments.arguments
>   local argumenttable = utilities.parsers.settings_to_hash(argumentlist)
>
>   context(argumenttable["fname"])

Many thanks for your reply, Wolfgang.

This is exactly what I need.

Many thanks for your help,

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 17:06   ` Pablo Rodriguez
@ 2020-10-13 17:14     ` Wolfgang Schuster
  2020-10-13 17:38       ` Pablo Rodriguez
  0 siblings, 1 reply; 8+ messages in thread
From: Wolfgang Schuster @ 2020-10-13 17:14 UTC (permalink / raw)
  To: mailing list for ConTeXt users; +Cc: Pablo Rodriguez

Pablo Rodriguez schrieb am 13.10.2020 um 19:06:
> On 10/13/20 5:54 PM, Aditya Mahajan wrote:
>> On Tue, 13 Oct 2020, Pablo Rodriguez wrote:
>>> [...]
>>> Could anyone explain me what I’m missing here?
> Many thanks for your reply, Aditya.
>
>> I find it simpler to write macros where there is clean communicating
>> between tex and lua. For example:
>> [...]
>>      \define[1]\numbersfname
>>          {\cldcontext{userdata.numbersfname([==[#1]==])}}
> This is the first time I see that way of wrapping an argument.
>
> In any case, I cannot compare to a number:
>
>    \startluacode
>      userdata = userdata or {}
>      function userdata.numbersfname(name)
>          return string.match(name, "%d+")
>      end
>    \stopluacode
>
>    \define[1]\numbersfname
>        {\cldcontext{userdata.numbersfname([==[#1]==])}}

Untested:

\defineexpandable[1]\numbersfname
   {\cldcontext{userdata.numbersfname([==[#1]==])}}

Wolfgang

___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 15:33 need help comparing strings Pablo Rodriguez
  2020-10-13 15:54 ` Aditya Mahajan
  2020-10-13 16:14 ` Wolfgang Schuster
@ 2020-10-13 17:16 ` Pablo Rodriguez
  2 siblings, 0 replies; 8+ messages in thread
From: Pablo Rodriguez @ 2020-10-13 17:16 UTC (permalink / raw)
  To: mailing list for ConTeXt users

On 10/13/20 5:33 PM, Pablo Rodriguez wrote:
> [...]
>   \starttext
>   \ctxlua{ d = [[\env{fname}]] }
>
>   d: \cldcontext{d}\\
>   \doifelse{\cldcontext{d:match("\letterpercent d+")}}
>       {123}{yes}{no}
>   \stoptext
> [...]
> 3. I’m trying this on a Linux box, but on Windows I would say that Lua
> variable "d" is empty.

Just for the record, this sample also works in Windows.

The Lua variable isn’t empty and the conditional outputs "yes".

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

* Re: need help comparing strings
  2020-10-13 17:14     ` Wolfgang Schuster
@ 2020-10-13 17:38       ` Pablo Rodriguez
  0 siblings, 0 replies; 8+ messages in thread
From: Pablo Rodriguez @ 2020-10-13 17:38 UTC (permalink / raw)
  To: ntg-context

On 10/13/20 7:14 PM, Wolfgang Schuster wrote:
> Pablo Rodriguez schrieb am 13.10.2020 um 19:06:
>> [...]
>> In any case, I cannot compare to a number:
>>
>>    \startluacode
>>      userdata = userdata or {}
>>      function userdata.numbersfname(name)
>>          return string.match(name, "%d+")
>>      end
>>    \stopluacode
>>
>>    \define[1]\numbersfname
>>        {\cldcontext{userdata.numbersfname([==[#1]==])}}
>
> Untested:
>
> \defineexpandable[1]\numbersfname
>    {\cldcontext{userdata.numbersfname([==[#1]==])}}

I’m afraid it doesn’t work.

  \startluacode
    userdata = userdata or {}
    function userdata.numbersfname(name)
        return string.match(name, "%d+")
    end
  \stopluacode

  \defineexpandable[1]\numbersfname
    {\cldcontext{userdata.numbersfname([==[#1]==])}}

  \starttext
  \numbersfname{abc123}

  \numbersfname{\env{fname}}
  \doifelse{\numbersfname{\env{fname}}}{123}{yes}{no}
  \stoptext

Just curious about what the difference might be.

Many thanks for your help,

Pablo
--
http://www.ousia.tk
___________________________________________________________________________________
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://context.aanhet.net
archive  : https://bitbucket.org/phg/context-mirror/commits/
wiki     : http://contextgarden.net
___________________________________________________________________________________

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

end of thread, other threads:[~2020-10-13 17:38 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-10-13 15:33 need help comparing strings Pablo Rodriguez
2020-10-13 15:54 ` Aditya Mahajan
2020-10-13 17:06   ` Pablo Rodriguez
2020-10-13 17:14     ` Wolfgang Schuster
2020-10-13 17:38       ` Pablo Rodriguez
2020-10-13 16:14 ` Wolfgang Schuster
2020-10-13 17:09   ` Pablo Rodriguez
2020-10-13 17:16 ` Pablo Rodriguez

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).