having trouble with using the utils.split

Posted by Dragonlord on Tue 05 Apr 2016 02:09 AM — 12 posts, 40,309 views.

#0
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>
Amended on Tue 05 Apr 2016 08:56 PM by Nick Gammon
USA Global Moderator #1
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.
Amended on Tue 05 Apr 2016 08:43 AM by Fiendish
#2
okay this is the error i am getting


Compile error
World: Materia Magica GMCP
Immediate execution
[string "Alias: G_Reagent_items"]:22: ')' expected near '='
Amended on Tue 05 Apr 2016 08:55 PM by Nick Gammon
USA Global Moderator #3
"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.
Amended on Tue 05 Apr 2016 03:15 PM by Fiendish
#4
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>
Amended on Tue 05 Apr 2016 08:56 PM by Nick Gammon
Australia Forum Administrator #5
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.
Amended on Tue 05 Apr 2016 08:55 PM by Nick Gammon
Australia Forum Administrator #6
Template:codetag
To make your code more readable please use [code] tags as described here.
#7
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>
USA Global Moderator #8
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
Amended on Tue 05 Apr 2016 10:27 PM by Fiendish
USA Global Moderator #9
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.
Amended on Tue 05 Apr 2016 10:37 PM by Fiendish
Australia Forum Administrator #10

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.
#11
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