Pattern matching
I try to extract the itemId from a loot message but am not sure how to do this.
For example I have the following loot message. "You receive loot: |cffffffff|Hitem:2589::::::::13:::::::|h[Linen Cloth]|h|r." I use the this funtion to extract the number: Lua Code:
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? |
local _, _, itemID = strsplit(":", line)
This will split the message string by colons, making the item ID the third return in this message string. It gives you a string, so if you want a number: itemID = tonumber(itemID) |
Perfect. Thank you Kanegasi :)
|
As a side note, the following line can be rewritten.
Code:
if CA_match_number ~= nil then Code:
if CA_match_number then Conversely, think about this line. Code:
if not CA_match_number then Taking it a step further, you can expand if/then to check for having a value, false, and nil. Code:
if something then -- true or has a value, and not false and is non-nil |
Follow up, to make things a bit more clear. The two following sets of lines are functionally identical but are not exactly identical.
Code:
if something then Code:
if not something then |
Quote:
Code:
return tonumber(line:match("|Hitem:(%d+)")) or 0; |
All times are GMT -6. The time now is 06:17 AM. |
vBulletin © 2024, Jelsoft Enterprises Ltd
© 2004 - 2022 MMOUI