Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.

Due to spam on this forum, all posts now need moderator approval.

 Entire forum ➜ MUSHclient ➜ General ➜ table.insert and string.gsub

table.insert and string.gsub

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Mahony   (27 posts)  Bio
Date Tue 23 Sep 2014 12:46 PM (UTC)
Message
I'm trying to insert a string that I need to adjust before entering.


%1 is amulet aardwolf                            |

t = {}
table.insert(t, "string.gsub("%1","( +)|","")")

I get error
[string "Trigger: "]:2: ')' expected near 'amulet'


I read lue table details and help for lua tables and searched forum...

When I do it this way

%1 is amulet aardwolf                            |
aaa = string.gsub("%1","( +)|","")
t = {}
table.insert(t, "aaa")


it works. But I don't want to use another variable and I think it must work the first way... somehow
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #1 on Tue 23 Sep 2014 01:22 PM (UTC)

Amended on Tue 23 Sep 2014 01:23 PM (UTC) by Fiendish

Message
Quote:
But I don't want to use another variable

That is a bad reason.


Anyway, in your first attempt you should change
table.insert(t, "string.gsub("%1","( +)|","")")

to
table.insert(t, string.gsub("%1","( +)|",""))




Also, your second attempt doesn't actually work.
You want
table.insert(t, aaa)

not
table.insert(t, "aaa")

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Mahony   (27 posts)  Bio
Date Reply #2 on Tue 23 Sep 2014 01:43 PM (UTC)

Amended on Tue 23 Sep 2014 01:45 PM (UTC) by Mahony

Message
here is what i get if I omit the quotes

table.insert(t, string.gsub("%1","( +)|",""))

Run-time error
World: Aardwolf
Immediate execution
[string "Trigger: "]:6: bad argument #2 to 'insert' (number expected, got string)
stack traceback:
        [C]: in function 'insert'
        [string "Trigger: "]:6: in main chunk


I played with all sorts of quotes and didn't get the result.. :(
You are right with the second part

table.insert(t, aaa)

is correct and works.
Top

Posted by Fiendish   USA  (2,534 posts)  Bio   Global Moderator
Date Reply #3 on Tue 23 Sep 2014 06:46 PM (UTC)
Message
Mahony said:

here is what i get if I omit the quotes

table.insert(t, string.gsub("%1","( +)|",""))

Run-time error
World: Aardwolf
Immediate execution
[string "Trigger: "]:6: bad argument #2 to 'insert' (number expected, got string)
stack traceback:
        [C]: in function 'insert'
        [string "Trigger: "]:6: in main chunk



Ah...of course. Sorry, I wasn't thinking. gsub returns multiple values. So you can't directly pass it to table.insert. What the second one is doing is implicitly dropping the second return value from gsub.

https://github.com/fiendish/aardwolfclientpackage
Top

Posted by Nick Gammon   Australia  (23,133 posts)  Bio   Forum Administrator
Date Reply #4 on Tue 23 Sep 2014 08:33 PM (UTC)

Amended on Tue 23 Sep 2014 08:36 PM (UTC) by Nick Gammon

Message
What's your regexp doing? You can't use the "|" in Lua regexps (unless you are looking for "|"). In PCRE regexeps the "|" symbol means "or".


foo = "amulet          aardwolf"

t = {}
table.insert(t, (string.gsub(foo," +"," ")))

require "tprint"
tprint (t)


That example removes the spaces in foo. The brackets around string.gsub forces the return of the first result only.

[EDIT] Ah I see now. Try this:


foo = "amulet aardwolf                            |"

t = {}
table.insert(t, (string.gsub(foo," +|","")))

tprint (t)


Replace foo by "%1" and it should work, eg.


t = {}
table.insert(t, (string.gsub("%1"," +|","")))

- Nick Gammon

www.gammon.com.au, www.mushclient.com
Top

Posted by Mahony   (27 posts)  Bio
Date Reply #5 on Tue 23 Sep 2014 09:41 PM (UTC)
Message
It works! :) Thank you Nick
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


16,567 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.