[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  MUSHclient
. -> [Folder]  Lua
. . -> [Subject]  having trouble with using the utils.split

having trouble with using the utils.split

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


Posted by Dragonlord   (24 posts)  [Biography] bio
Date Tue 05 Apr 2016 02:09 AM (UTC)

Amended on Tue 05 Apr 2016 08:56 PM (UTC) by Nick Gammon

Message
I have tried a number of different ways of using the utils.split and each time i am getting errors this is my tenth scripts/aliases



<aliases>
  <alias
   name="G_Reagent_items"
   match="G_Reagent_items"
   enabled="y"
   group="G_Reagent_items"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>Send ("open itemg")
Send ("get all itemg")
Send ("close itemg")

whichReagent = string.match (item_generator, "^reagent%-(%a+)$")

if whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "airreagents")
elseif whichReagent then
  Send ("put all\.", G_Reagents, " " .. whichReagent .. "earthreagents")
elseif whichReagent then
  Send ("put all\.", G_Reagents, " " .. whichReagent .. "waterreagents")
elseif whichReagent then
  Send ("put all\.", G_Reagents, " " .. whichReagent .. "firereagents")
elseif whichReagent then
  Send ("put all\.", G_Reagents, " " .. whichReagent .. "gemreagents")
elseif whichReagent then
  Send ("put all\.", G_Reagents, " " .. whichReagent .. "etherealreagents")
end  -- if

function item_generator (
  G_Reagents = "water|fire|air|earth|gem|ethereal"  
  item_generator = utils.split (G_Reagents, "|")
end)</send>
  </alias>
</aliases>
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #1 on Tue 05 Apr 2016 07:47 AM (UTC)

Amended on Tue 05 Apr 2016 08:43 AM (UTC) by Fiendish

Message
Instead of saying "i am getting errors", can you please tell us what the error messages say when asking for help?
It's not nice to make us go through that exercise ourselves.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Dragonlord   (24 posts)  [Biography] bio
Date Reply #2 on Tue 05 Apr 2016 02:57 PM (UTC)

Amended on Tue 05 Apr 2016 08:55 PM (UTC) by Nick Gammon

Message
okay this is the error i am getting


Compile error
World: Materia Magica GMCP
Immediate execution
[string "Alias: G_Reagent_items"]:22: ')' expected near '='
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #3 on Tue 05 Apr 2016 03:11 PM (UTC)

Amended on Tue 05 Apr 2016 03:15 PM (UTC) by Fiendish

Message
"function item_generator (" and "end)" should be "function item_generator()" and "end"

Your error message is saying, essentially, "a ) was expected but not found on line 22 of the alias code". This is because on line 21 you have just ( instead of (). Because you threw that ) way down after the end instead of where it's supposed to go.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Dragonlord   (24 posts)  [Biography] bio
Date Reply #4 on Tue 05 Apr 2016 07:48 PM (UTC)

Amended on Tue 05 Apr 2016 08:56 PM (UTC) by Nick Gammon

Message
now it is saying this error


Compile error
World: Materia Magica GMCP
Immediate execution
[string "Alias: G_Reagent_items"]:3: '=' expected near 'whichReagent'


<aliases>
  <alias
   name="G_Reagent_items"
   match="G_Reagent_items"
   enabled="y"
   group="G_Reagent_items"
   regexp="y"
   send_to="12"
   ignore_case="y"
   sequence="100"
  >
  <send>RContainers

-- no ideal what is wrong with this line :(

whichReagent = string.match (G_Reagent, "^reagent%-(%a+)$")


if whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "airreagents")
elseif whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "earthreagents")
elseif whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "waterreagents")
elseif whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "firereagents")
elseif whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "gemreagents")
elseif whichReagent then
  Send ("put all\.", item_generator, " " .. whichReagent .. "etherealreagents")
end  -- if

function RContainers()
  Send ("open itemg")
  Send ("get all itemg")
  Send ("close itemg")
end -- RContainers

function item_generator()
  G_Reagents = "water|fire|air|earth|gem|ethereal"  
  item_generator = utils.split (G_Reagents, "|")
end</send>
  </alias>
</aliases>
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #5 on Tue 05 Apr 2016 08:53 PM (UTC)

Amended on Tue 05 Apr 2016 08:55 PM (UTC) by Nick Gammon

Message
Look at the line before:


RContainers

-- no ideal what is wrong with this line :(

whichReagent = string.match (G_Reagent, "^reagent%-(%a+)$")


What's RContainers doing there?

Getting rid of the comment and blank lines you have:



RContainers whichReagent = string.match (G_Reagent, "^reagent%-(%a+)$")


And that doesn't make any sense. :)




Do you mean:


Send ("RContainers")


or:


RContainers ()


You have to use the parentheses when calling functions, even if passing no arguments.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Tue 05 Apr 2016 08:54 PM (UTC)
Message
Template:codetag To make your code more readable please use [code] tags as described here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Dragonlord   (24 posts)  [Biography] bio
Date Reply #7 on Tue 05 Apr 2016 09:53 PM (UTC)
Message
Okay but my original question on this error. what I am trying to do is get utils.split to work, so I created this function with these in the function

G_Reagents = "reagent-water|reagent-fire|reagent-air|reagent-earth|gem|reagent-ethereal"

function item_generator()
G_Reagents = "water|fire|air|earth|gem|ethereal"
item_generator = utils.split (G_Reagents, "|")
end


Run-time error
World: Materia Magica GMCP
Immediate execution
[string "Alias: G_Reagent_items"]:5: bad argument #1 to 'match' (string expected, got function)
stack traceback:
[C]: in function 'match'
[string "Alias: G_Reagent_items"]:5: in main chunk


<aliases>
<alias
name="G_Reagent_items"
match="G_Reagent_items"
enabled="y"
group="G_Reagent_items"
regexp="y"
send_to="12"
ignore_case="y"
sequence="100"
>
<send>Send ("open itemg")
Send ("get all itemg")
Send ("close itemg")

whichReagent = string.match (item_generator, "^reagent%-(%a+)$")


if whichReagent then
Send ("put all\.", item_generator, " " .. whichReagent .. "airreagents")
elseif whichReagent then
Send ("put all\.", item_generator, " " .. whichReagent .. "earthreagents")
elseif whichReagent then
Send ("put all\.", item_generator, " " .. whichReagent .. "waterreagents")
elseif whichReagent then
Send ("put all\.", item_generator, " " .. whichReagent .. "firereagents")
elseif whichReagent then
Send ("put all\.", item_generator, " " .. whichReagent .. "gemreagents")
elseif whichReagent then
Send ("put all\.", item_generator, " " .. whichReagent .. "etherealreagents")
end -- if


function item_generator()
G_Reagents = "water|fire|air|earth|gem|ethereal"
item_generator = utils.split (G_Reagents, "|")
end</send>
</alias>
</aliases>
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #8 on Tue 05 Apr 2016 10:08 PM (UTC)

Amended on Tue 05 Apr 2016 10:27 PM (UTC) by Fiendish

Message
I'm going to answer your question and then give some advice.

First the answer:

You wrote this.
function item_generator() ... end

That defines a thing called item_generator that is a Function. Then INSIDE of that function you try setting item_generator to be a string? Why are you using the same name there for something extremely radically different?

When you do

string.match (item_generator, ...)

item_generator is not a string. We know this because the error message says so.



Now the advice:

Never use utils.split if you have pre-defined the set of things. Only use it if you have some sort of dynamic information that you have no control over and that happens to be in a known format.

Don't do this!

my_reagents_table = utils.split ("water|fire|air|earth|gem|ethereal" , "|")


Just do this!

my_reagents_table = {"water","fire","air","earth","gem","ethereal"}



Second advice:

Stop escaping those periods in your Sends.
"\." is an invalid string escape sequence. Invalid escape sequences turn into dangerously ambiguous behavior.
Don't do it.


Third advice:

This is not a good way to learn how to program in Lua.
You can't just throw random symbols at MUSHclient and hope for the best.
I suggest starting first with something like http://luatut.com/crash_course.html
Then head over to the more complete http://lua-users.org/wiki/TutorialDirectory

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Fiendish   USA  (2,514 posts)  [Biography] bio   Global Moderator
Date Reply #9 on Tue 05 Apr 2016 10:36 PM (UTC)

Amended on Tue 05 Apr 2016 10:37 PM (UTC) by Fiendish

Message
How to read this error message...
Quote:
[string "Alias: G_Reagent_items"]:5: bad argument #1 to 'match' (string expected, got function)


Quote:
[string "Alias: G_Reagent_items"]

In an alias called G_Reagent_items.

Quote:
:5:

On line 5.

Quote:
bad argument #1 to 'match'

You called a function called "match" and gave it a bad first argument. "match" in this case is a member method of the string object class. We know this because we can see that on line 5 you call string.match.

Quote:
(string expected, got function)

The first argument to the match function should have been a string, but you gave something that isn't a string. You gave another function.

https://github.com/fiendish/aardwolfclientpackage
[Go to top] top

Posted by Nick Gammon   Australia  (22,973 posts)  [Biography] bio   Forum Administrator
Date Reply #10 on Wed 06 Apr 2016 05:59 AM (UTC)
Message

function item_generator()
G_Reagents = "water|fire|air|earth|gem|ethereal"
item_generator = utils.split (G_Reagents, "|")
end


This looks like a Basic programmer let loose on Lua. :)

You don't assign to the function to return a value. You do this instead:


function item_generator()
  G_Reagents = "water|fire|air|earth|gem|ethereal"
  return utils.split (G_Reagents, "|")
end


However as Fiendish said, this is a rather roundabout way of specifying a table, which you could have done in the first place.

BTW did you read this link?

Template:codetag To make your code more readable please use [code] tags as described here.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Dragonlord   (24 posts)  [Biography] bio
Date Reply #11 on Fri 08 Apr 2016 05:21 PM (UTC)
Message
I am doing something that I haven't in years. That is I have pulled out a book on programming - beginners programming for LUA. I have learned to program in basic, c/c++... now this :P
[Go to top] 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.


24,302 views.

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

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

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

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]