A problem with a table

Posted by Shaun Biggs on Tue 28 Aug 2007 07:15 AM — 5 posts, 18,945 views.

USA #0
I am trying to have a plugin with changeable display colours. I'm having a large problem with the colour changing script though, and I can't figure out why.

function ChangeColour( sTrig, sLine, wildcards )
  print( wildcards[1].." "..wildcards[2] )
  require "tprint"
  tprint( colours )
  tprint( colourcodes )
  print( ( colours[ wildcards[1] ] or "nil" ).." "..( colourcodes[ wildcards[2] or "" ] or "nil" ) )
  if colourcodes[ wildcards[2] ] ~= nil then
    colours[ wildcards[1] ] = wildcards[2]
  else
    ColourNote( colourcodes[ "%W" ], "", wildcards[2].." is an invalid selection " )
  end
end

all the prints are just for debugging this odd problem. Here is what happens when it is run:

1 @R
1="@G"
2="@W"
"@B"="#78BCFF"
"@C"="#5AFFFF"
"@m"="#BE32FF"
"@y"="#E3BF65"
"@W"="white"
"@M"="#FF5AFF"
"@b"="#5E5EFF"
"@c"="cyan"
"@R"="#FF5A5A"
"@G"="palegreen"
"@r"="red"
"@Y"="#FFFF5A"
"@w"="#E1E1E1"
"@g"="#2CCE2C"
nil #FF5A5A

somehow colours[1] is showing up as nil when printed using the wildcard, but not when going through the pairs.
Australia Forum Administrator #1
What pairs is that?

Can you tprint the wildcards?
USA #2

  require "tprint"
  tprint( wildcards )
  tprint( colours )
  tprint( colourcodes )
...


1="1"
2="@R"
0="gain:col1 @R"
1="@G"
2="@W"
"@B"="#78BCFF"
"@C"="#5AFFFF"
"@m"="#BE32FF"
"@y"="#E3BF65"
"@W"="white"
"@M"="#FF5AFF"
"@b"="#5E5EFF"
"@c"="cyan"
"@R"="#FF5A5A"
"@G"="palegreen"
"@r"="red"
"@Y"="#FFFF5A"
"@w"="#E1E1E1"
"@g"="#2CCE2C"
nil #FF5A5A

USA #3
Ok, finally got it figured out... it seems that the only time that Lua doesn't take care of type casting for you is with table keys. this now works:

function ChangeColour( sTrig, sLine, wildcards )
  if colourcodes[ wildcards[2] ] ~= nil then
    colours[ tonumber( wildcards[1] ) ] = wildcards[2]
    ColourNote( colourcodes[ "@W" ],         "", "Colour "..wildcards[1].." set to ",
                colourcodes[ wildcards[2] ], "", wildcards[2] )
  else
    ColourNote( colourcodes[ "@W" ], "", wildcards[2].." is an invalid selection " )
  end
end
Amended on Tue 28 Aug 2007 09:05 AM by Shaun Biggs
Australia Forum Administrator #4
Tables can be keyed on strings or numbers (amongst other things), so it can't afford to type-cast for you.