string search from back to front?

classic Classic list List threaded Threaded
21 messages Options
12
Reply | Threaded
Open this post in threaded view
|

string search from back to front?

meino.cramer
Hi,

in the PIL 2ed I didn't find a way to search from the end of a string
to the beginning. There is a way to specify an offset from the back
with a negative instead of a positiv, but from that specified point
the search goes from 'left to right' or 'front to back'.
Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
something ?

Best regards,
mcc




Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Petite Abeille

On Oct 28, 2013, at 7:42 PM, [hidden email] wrote:

> in the PIL 2ed

Use the manual:

http://www.lua.org/manual/5.2/

> I didn't find a way to search from the end of a string
> to the beginning. There is a way to specify an offset from the back
> with a negative instead of a positiv, but from that specified point
> the search goes from 'left to right' or 'front to back'.

http://www.lua.org/manual/5.2/manual.html#pdf-string.reverse

> Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
> something ?

http://www.lua.org/manual/5.2/manual.html#pdf-string.sub

Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Leo Razoumov
In reply to this post by meino.cramer
On Mon, Oct 28, 2013 at 2:42 PM,  <[hidden email]> wrote:

> Hi,
>
> in the PIL 2ed I didn't find a way to search from the end of a string
> to the beginning. There is a way to specify an offset from the back
> with a negative instead of a positiv, but from that specified point
> the search goes from 'left to right' or 'front to back'.
> Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
> something ?
>
> Best regards,
> mcc

Lua pattern matching is from left-to-right with a flexible choice of
the starting position and special markers like ^ and $ for beginning
and end of a string. In most cases it is sufficient. If you *really*
need to match from right-to-left you can use string.reverse(s) and
apply reversed pattern to it. But in my experience, unless you are
doing something contrive, Lua's pattern matching rules are just fine.
Remember, you can use '$' to peg your pattern to the end-of-string.

--Leo--

Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

meino.cramer
In reply to this post by Petite Abeille
Petite Abeille <[hidden email]> [13-10-29 02:28]:

>
> On Oct 28, 2013, at 7:42 PM, [hidden email] wrote:
>
> > in the PIL 2ed
>
> Use the manual:
>
> http://www.lua.org/manual/5.2/
>
> > I didn't find a way to search from the end of a string
> > to the beginning. There is a way to specify an offset from the back
> > with a negative instead of a positiv, but from that specified point
> > the search goes from 'left to right' or 'front to back'.
>
> http://www.lua.org/manual/5.2/manual.html#pdf-string.reverse
>
> > Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
> > something ?
>
> http://www.lua.org/manual/5.2/manual.html#pdf-string.sub
>


I am using Lua 5.1.5, thats why I am reading PIL 2ed.

Best regards,
mcc



Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Choonster TheMage

On 29 Oct 2013 12:33, <[hidden email]> wrote:
>
> Petite Abeille <[hidden email]> [13-10-29 02:28]:
> >
> > On Oct 28, 2013, at 7:42 PM, [hidden email] wrote:
> >
> > > in the PIL 2ed
> >
> > Use the manual:
> >
> > http://www.lua.org/manual/5.2/
> >
> > > I didn't find a way to search from the end of a string
> > > to the beginning. There is a way to specify an offset from the back
> > > with a negative instead of a positiv, but from that specified point
> > > the search goes from 'left to right' or 'front to back'.
> >
> > http://www.lua.org/manual/5.2/manual.html#pdf-string.reverse
> >
> > > Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
> > > something ?
> >
> > http://www.lua.org/manual/5.2/manual.html#pdf-string.sub
> >
>
>
> I am using Lua 5.1.5, thats why I am reading PIL 2ed.
>
> Best regards,
> mcc
>
>
>

5.1 has a manual too:
http://www.lua.org/manual/5.1/manual.html

Most of the library functions are the same between 5.1 and 5.2.

Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

meino.cramer
Choonster TheMage <[hidden email]> [13-10-29 03:12]:

> On 29 Oct 2013 12:33, <[hidden email]> wrote:
> >
> > Petite Abeille <[hidden email]> [13-10-29 02:28]:
> > >
> > > On Oct 28, 2013, at 7:42 PM, [hidden email] wrote:
> > >
> > > > in the PIL 2ed
> > >
> > > Use the manual:
> > >
> > > http://www.lua.org/manual/5.2/
> > >
> > > > I didn't find a way to search from the end of a string
> > > > to the beginning. There is a way to specify an offset from the back
> > > > with a negative instead of a positiv, but from that specified point
> > > > the search goes from 'left to right' or 'front to back'.
> > >
> > > http://www.lua.org/manual/5.2/manual.html#pdf-string.reverse
> > >
> > > > Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
> > > > something ?
> > >
> > > http://www.lua.org/manual/5.2/manual.html#pdf-string.sub
> > >
> >
> >
> > I am using Lua 5.1.5, thats why I am reading PIL 2ed.
> >
> > Best regards,
> > mcc
> >
> >
> >
>
> 5.1 has a manual too:
> http://www.lua.org/manual/5.1/manual.html
>
> Most of the library functions are the same between 5.1 and 5.2.

Lua 5.1.5 cannot do real backward search.

Best regards,
mcc




Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Petite Abeille
In reply to this post by meino.cramer

On Oct 29, 2013, at 2:32 AM, [hidden email] wrote:

> I am using Lua 5.1.5, thats why I am reading PIL 2ed.

Two suggestions:

(1) Read PIL 3ed
(2) Use the manual either way

One hint:
(1) http://www.lua.org/manual/5.1/


Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Petite Abeille
In reply to this post by meino.cramer

On Oct 29, 2013, at 3:16 AM, [hidden email] wrote:

> Lua 5.1.5 cannot do real backward search.

What's 'real backward search'?

But whatever that is, perhaps there is an alternative way to tackle the problem at hand. What are you trying to achieve?
Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Dirk Laurie-2
In reply to this post by meino.cramer
s='the quick brown fox jumps over the lazy dog'
w='the'
j,k=s:reverse():find(w:reverse()) --> 10 12
j,k=#s-k+1,#s-j+1  --> 32 34
print(s:sub(j,k))  --> 'the

Just turn that into a function, and there you are.


2013/10/28  <[hidden email]>:

> Hi,
>
> in the PIL 2ed I didn't find a way to search from the end of a string
> to the beginning. There is a way to specify an offset from the back
> with a negative instead of a positiv, but from that specified point
> the search goes from 'left to right' or 'front to back'.
> Is there real;y no equivalent to "RIGHTSTR()" in lua or did I overlook
> something ?
>
> Best regards,
> mcc
>
>
>
>

Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Luiz Henrique de Figueiredo
How about this?

s='the quick brown fox jumps over the lazy dog'
w='the'

print(s:find(w)) -- first match (position is first return value)
print(s:find('.*()'..w)) -- last match (position is third return value)

Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

David Favro
On 10/30/2013 10:59 AM, Luiz Henrique de Figueiredo wrote:
> How about this?
>
> s='the quick brown fox jumps over the lazy dog'
> w='the'
>
> print(s:find(w)) -- first match (position is first return value)
> print(s:find('.*()'..w)) -- last match (position is third return value)

Just an implementation detail, but I think it would be advisable to tie it
to the start of the string, making the case of failed matches much quicker;
I believe it produces the same results:
print(s:find('^.*()'..w))

> s='the quick brown fox jumps over the lazy dog'
> w='the'
> sstr='.*()'..w
> c1=os.clock() for i=1,999999 do s:find(sstr) end print(os.clock()-c1)
0.68
> w='foo'
> sstr='.*()'..w
> c1=os.clock() for i=1,999999 do s:find(sstr) end print(os.clock()-c1)
26.53
> sstr='^.*()'..w
> c1=os.clock() for i=1,999999 do s:find(sstr) end print(os.clock()-c1)
1.33


Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Luiz Henrique de Figueiredo
> Just an implementation detail, but I think it would be advisable to tie it
> to the start of the string, making the case of failed matches much quicker;
> I believe it produces the same results:
> print(s:find('^.*()'..w))

Excellent point!

Reply | Threaded
Open this post in threaded view
|

__declared

Jose Torre-Bueno
In reply to this post by David Favro

Poking about I discovered that the metatable for _G (or _ENV) has a table called __declared whose keys are the names of user defined objects and value is always true.  This seems a useful thing for testing whether a function has been defined or a table exists.

I am not aware of any documentation of this feature, __declared does not appear in the Lua 5.2 reference manual or in the index of PiL 3rd edition.  Is this a documented feature of Lua? Can I rely on this in the future?  Also do the __newindex and __index functions in the metatable do anything other than maintain __declared?

I discovered this in the process of studying whether I could create a function that incremented a counter for every access of a function.  I have in mind a function installed as the __index function of the metatable that would maintain a table similar to __declared except that it would increment a counter for the called function every time it was called.  if this function ended by calling the preexisting __index function would that work without breaking Lua?

Jose de la Torre-Bueno, Ph.D.
Empowered Energy Solutions Inc.
Intellectual Property & Technology Management
T (619) 977-0553
F (760) 295-7119
[hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: __declared

Paige DePol
Jose,

I do not believe that is part of the Lua core. I could not find any reference to '__declared' in the lua-5.2.2 code base and a grep for just 'declared' only returns results that are comments. I also looked in my own environment table and could not find a '__declared' field.

Perhaps you have a patched Lua and/or some other module is creating the '__declared' key in your environment?

-Paige



On Oct 30, 2013, at 4:17 PM, Jose Torre-Bueno <[hidden email]> wrote:

>
> Poking about I discovered that the metatable for _G (or _ENV) has a table called __declared whose keys are the names of user defined objects and value is always true.  This seems a useful thing for testing whether a function has been defined or a table exists.
>
> I am not aware of any documentation of this feature, __declared does not appear in the Lua 5.2 reference manual or in the index of PiL 3rd edition.  Is this a documented feature of Lua? Can I rely on this in the future?  Also do the __newindex and __index functions in the metatable do anything other than maintain __declared?
>
> I discovered this in the process of studying whether I could create a function that incremented a counter for every access of a function.  I have in mind a function installed as the __index function of the metatable that would maintain a table similar to __declared except that it would increment a counter for the called function every time it was called.  if this function ended by calling the preexisting __index function would that work without breaking Lua?
>
> Jose de la Torre-Bueno, Ph.D.
> Empowered Energy Solutions Inc.
> Intellectual Property & Technology Management
> T (619) 977-0553
> F (760) 295-7119
> [hidden email]
>
>


Reply | Threaded
Open this post in threaded view
|

Re: __declared

Jose Torre-Bueno
In reply to this post by David Favro

On Oct 30, 2013, at 4:17 PM, Jose Torre-Bueno <[hidden email]> wrote:

>
> Poking about I discovered that the metatable for _G (or _ENV) has a table called __declared whose keys are the names of user defined objects and value is always true.  This seems a useful thing for testing whether a function has been defined or a table exists.

Even stranger sometimes I can get the metatable for _G and sometimes not as shown in this interpreter record:

Lua 5.2.0  Copyright (C) 1994-2011 Lua.org, PUC-Rio
> =getmetatable(_G)
nil
> require'utilities'
> =getmetatable(_G)
nil
> require'evolve2'
> = getmetatable(_G)
table: 0x100130730
>

utilities is 658 lines of Lua and evolve2 is 1533 lines, neither makes any C calls, also just loading them w/o running any functions is enough to make the metatable visible.

Jose de la Torre-Bueno, Ph.D.
Empowered Energy Solutions Inc.
Intellectual Property & Technology Management
T (619) 977-0553
F (760) 295-7119
[hidden email]


Reply | Threaded
Open this post in threaded view
|

Re: __declared

Jose Torre-Bueno
In reply to this post by Jose Torre-Bueno

On Oct 30, 2013, at 4:41 PM, Paige DePol <[hidden email]> wrote:

>
> I do not believe that is part of the Lua core. I could not find any reference to '__declared' in the lua-5.2.2 code base and a grep for just 'declared' only returns results that are comments. I also looked in my own environment table and could not find a '__declared' field.
>

You are correct I just figured out it is created by the add on strict.lua which adds a metatable to _G and uses it to prevent the accidental creation of globals inside functions.




Reply | Threaded
Open this post in threaded view
|

Re: __declared

Paige DePol
I am glad you found out where the issue was! :)

I was thinking it may of been part of some sort of global var tracking system. I patched the parser and added a 'global' keyword for the explicit declaration of global variables, though instead of storing my table in _G I stored it in the registry.

-Paige



On Oct 30, 2013, at 4:52 PM, Jose Torre-Bueno <[hidden email]> wrote:

>
> On Oct 30, 2013, at 4:41 PM, Paige DePol <[hidden email]> wrote:
>
>>
>> I do not believe that is part of the Lua core. I could not find any reference to '__declared' in the lua-5.2.2 code base and a grep for just 'declared' only returns results that are comments. I also looked in my own environment table and could not find a '__declared' field.
>>
>
> You are correct I just figured out it is created by the add on strict.lua which adds a metatable to _G and uses it to prevent the accidental creation of globals inside functions.
>
>
>
>
>


Reply | Threaded
Open this post in threaded view
|

Re: __declared

Jose Torre-Bueno
In reply to this post by Jose Torre-Bueno
On Oct 30, 2013, at 5:06 PM, Paige DePol <[hidden email]> wrote:

>
> I patched the parser and added a 'global' keyword for the explicit declaration of global variables, though instead of storing my table in _G I stored it in the registry.
>
> -Paige

Given that strict had created the __declared table inside _G I was able to make a function to list all globals like so:

function listglobals()
        local t = getmetatable(_G)
        for i,_ in pairs(t.__declared) do
                local ty = type(_G[i])
                local sz = 0
                if ty == 'table' then sz = #_G[i] end
                printf('%20s, %10s, %4d',i, ty, sz)
        end
end

Its remarkable how easy it is to do things like this in Lua.



Jose de la Torre-Bueno, Ph.D.
Empowered Energy Solutions Inc.
Intellectual Property & Technology Management
T (619) 977-0553
F (760) 295-7119
[hidden email]








Reply | Threaded
Open this post in threaded view
|

Re: string search from back to front?

Roberto Ierusalimschy
In reply to this post by Luiz Henrique de Figueiredo
> > Just an implementation detail, but I think it would be advisable to tie it
> > to the start of the string, making the case of failed matches much quicker;
> > I believe it produces the same results:
> > print(s:find('^.*()'..w))
>
> Excellent point!

Also, you can use s:match instead of s:find (and then you have only the
position as a result).

-- Roberto

Reply | Threaded
Open this post in threaded view
|

Re: __declared

Graham Wakefield-2
In reply to this post by Jose Torre-Bueno
I may be missing something, but a routine to print all globals is

for k, v in pairs(_G) do
        print(k, v)
        -- insert recursion if desired here…
end

(Obviously a fancier printer would memoize, handle loops, trace metatables, format prettily etc… but my point is that strict.lua isn't required to find globals.)

On Oct 31, 2013, at 10:09 AM, Jose Torre-Bueno <[hidden email]> wrote:

> On Oct 30, 2013, at 5:06 PM, Paige DePol <[hidden email]> wrote:
>
>>
>> I patched the parser and added a 'global' keyword for the explicit declaration of global variables, though instead of storing my table in _G I stored it in the registry.
>>
>> -Paige
>
> Given that strict had created the __declared table inside _G I was able to make a function to list all globals like so:
>
> function listglobals()
> local t = getmetatable(_G)
> for i,_ in pairs(t.__declared) do
> local ty = type(_G[i])
> local sz = 0
> if ty == 'table' then sz = #_G[i] end
> printf('%20s, %10s, %4d',i, ty, sz)
> end
> end
>
> Its remarkable how easy it is to do things like this in Lua.
>
>
>
> Jose de la Torre-Bueno, Ph.D.
> Empowered Energy Solutions Inc.
> Intellectual Property & Technology Management
> T (619) 977-0553
> F (760) 295-7119
> [hidden email]
>
>
>
>
>
>
>
>


12