Originally Posted by Yukyuk
This will work just fine is the itemId is the biggest number in the message.
But it will fail if there is a bigger number in the message.
It there a way to extract the first number from a text string?
|
Lua always returns the first match in a string. The problem with this is since the color code for the string is a hex value, it can contain numeric characters, which will sometimes get captured instead depending on the item quality. You can further define what to grab by defining the pattern better.
Code:
return tonumber(line:match("|Hitem:(%d+)")) or 0;
Notes: The parenthesis define what to capture in a pattern. This defaults to the entire pattern if no captures are defined. Also, this is a shortcut way to use
string.match() since the
string table is set as the metatable for all strings.
string.match() also returns strings exclusively if not
nil.
tonumber() converts this into a number and will pass
nil through.
or handles the next step by providing a default value if
nil is passed through. Lastly,
return is not a function, it's part of the language structure. It'll lead to some confusion further down the road if you write it like a function call.