string.findpairs?

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

string.findpairs?

Soni "They/Them" L.
Is there any way I can do

for from,to in string.findpairs("my string", ".") do
  print(from, to)
end
-- 
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.
Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Dirk Laurie-2
2014-12-12 14:59 GMT+02:00 Thiago L. <[hidden email]>:
> Is there any way I can do
>
> for from,to in string.findpairs("my string", ".") do
>   print(from, to)
> end

Do you mean this?
> _=string.gsub("The quick brown fox jumps over the lazy dog","()%S+()",print)
1    4
5    10
11    16
17    20
21    26
27    31
32    35
36    40
41    44

Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Tomás Guisasola-2
On Fri, 12 Dec 2014, Dirk Laurie wrote:

> 2014-12-12 14:59 GMT+02:00 Thiago L. <[hidden email]>:
>> Is there any way I can do
>>
>> for from,to in string.findpairs("my string", ".") do
>>   print(from, to)
>> end
>
> Do you mean this?
>> _=string.gsub("The quick brown fox jumps over the lazy dog","()%S+()",print)
> 1    4
> 5    10
> 11    16
> 17    20
> 21    26
> 27    31
> 32    35
> 36    40
> 41    44
>
>
  Or do you mean this?

> =string.gsub("The quick brown fox jumps over the lazy dog", "(%w+)%s+(%w+)", print)
The quick
brown fox
jumps over
the lazy
The quick brown fox jumps over the lazy dog 4
>

  Reagards,
  Tomás
Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Soni "They/Them" L.
In reply to this post by Dirk Laurie-2

On 12/12/14 11:11 AM, Dirk Laurie wrote:

> 2014-12-12 14:59 GMT+02:00 Thiago L. <[hidden email]>:
>> Is there any way I can do
>>
>> for from,to in string.findpairs("my string", ".") do
>>    print(from, to)
>> end
> Do you mean this?
>> _=string.gsub("The quick brown fox jumps over the lazy dog","()%S+()",print)
> 1    4
> 5    10
> 11    16
> 17    20
> 21    26
> 27    31
> 32    35
> 36    40
> 41    44
>
Can I do that with a for loop?

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Luiz Henrique de Figueiredo

> Can I do that with a for loop?

S="The quick brown fox jumps over the lazy dog"

for a,b in S:gmatch("(%S+)%s+(%S+)") do
        print(a,b)
end


Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Soni "They/Them" L.

On 12/12/14 11:33 AM, Luiz Henrique de Figueiredo wrote:
>> Can I do that with a for loop?
> S="The quick brown fox jumps over the lazy dog"
>
> for a,b in S:gmatch("(%S+)%s+(%S+)") do
> print(a,b)
> end
>
>
Ok what I want is more like

for a,b in S:gmatch("()%S+()") do

BUT I also want string.find-like results:

local from, to, match1, match2 = S:find("(%S+) (%S+)")

Can I get

S="The quick brown fox jumps over the lazy dog"
for from, to, match1, match2 in <something> do
   print(from, to, match1, match2)
end
--[[
1 9 The quick
11 19 brown fox
21 30 jumps over
32 39 the lazy
]]

(I find it more intuitive than `for from, match1, match2, to`)

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Dirk Laurie-2
2014-12-12 15:49 GMT+02:00 Thiago L. <[hidden email]>:
> (I find it more intuitive than `for from, match1, match2, to`)

function tweak(f)
   return function()
      local a,b,c,d = f()
      if d then return a,d-1,b,c end
   end
end

S="The quick brown fox jumps over the lazy dog"
pat="()(%S+)%s+(%S+)()"

for from,to,match1,match2 in tweak(S:gmatch(pat)) do
   print(from, to, match1, match2)
end

Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Soni "They/Them" L.

On 12/12/14 01:43 PM, Dirk Laurie wrote:

> 2014-12-12 15:49 GMT+02:00 Thiago L. <[hidden email]>:
>> (I find it more intuitive than `for from, match1, match2, to`)
> function tweak(f)
>     return function()
>        local a,b,c,d = f()
>        if d then return a,d-1,b,c end
>     end
> end
>
> S="The quick brown fox jumps over the lazy dog"
> pat="()(%S+)%s+(%S+)()"
>
> for from,to,match1,match2 in tweak(S:gmatch(pat)) do
>     print(from, to, match1, match2)
> end
>
What if I want to be able to stick any pattern on it without having to
write a new one every time?

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.


Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Dirk Laurie-2
2014-12-12 17:57 GMT+02:00 Thiago L. <[hidden email]>:

> What if I want to be able to stick any pattern on it without having to write
> a new one every time?

What about putting some thought into answering your own questions
before running to the list? You are not a beginner by any means.

Reply | Threaded
Open this post in threaded view
|

Re: string.findpairs?

Soni "They/Them" L.

On 12/12/14 04:18 PM, Dirk Laurie wrote:
> 2014-12-12 17:57 GMT+02:00 Thiago L. <[hidden email]>:
>
>> What if I want to be able to stick any pattern on it without having to write
>> a new one every time?
> What about putting some thought into answering your own questions
> before running to the list? You are not a beginner by any means.
>
I was having issues making it work properly... but I finally managed to
do it (stateless)

local findpairs
do
   local function _findnext(stringandpattern, index)
     -- only branch if index = 0
     if index > 0 then
       -- this should always give a match,
       -- as we're starting from the same index as the returned match
       local x,y = string.find(stringandpattern[1], stringandpattern[2],
index)
       return string.find(stringandpattern[1], stringandpattern[2], y+1)
     else
       return string.find(stringandpattern[1], stringandpattern[2],
index + 1)
     end
   end
   function findpairs(string, pattern)
     return _findnext, {string, pattern}, 0
   end
end

--
Disclaimer: these emails are public and can be accessed from <TODO: get a non-DHCP IP and put it here>. If you do not agree with this, DO NOT REPLY.