help with table.insert

Posted by AquaBrother on Mon 22 Nov 2010 05:05 PM — 7 posts, 24,877 views.

Brazil #0
Hi, i'm trying to do something i should not, hehe, once i have not enough experience on doing it...

Well, i'm trying to make a search in an especific table but i cannot find the right command to use...

I have a table called {friends}

I use the table.insert to insert new friends name in that table, i've read about a few functions on the internet that would find that name but, what i really need is making it search all name that fits with a sentence, eg:

if i type for example:

friend mike

and lez imagine that i have two friends named mike and mikein, i'd like that search to inform me that there are two names within the setence i just type...

Is that possible?

big trouble huh? if you feel like too busy or like it's too much trouble for answering that, nevermind...

Thx in advance
USA Global Moderator #1
I don't exactly understand. Is "friend mike" the sentence?
Can you give a better example of what you want to type and what you want to get as a result?
Brazil #2
hehe, ok, i'll try to explain again...

I have the following table already made:

Mike
Mikein
Milan
Mordon
ETC...

I wanna type:

Friend mike

I wanna get as result:

There are two mike in your table:
Mike
Mikein

USA Global Moderator #3
Off the top of my head, assuming you have along the lines of
friends = {"Mike", "Mikein", "Mikeout", "Wanda", "Jeff"}

I think you could make an alias with the pattern "friend *" that does the following:


local result = {}
local name = string.lower(%1)
for i,v in ipairs(friends) do
   if string.find(string.lower(v),name) ~= nil then
      table.insert(result,v)
   end
end
print("There are "..#result.." "..%1.." in your table:")
for i,v in ipairs(result) do
   print(v)
end


You may want to replace the print commands with something like Note or ColourNote.
Amended on Mon 22 Nov 2010 05:56 PM by Fiendish
Brazil #4
hehe, thx Fiendish i appreciate your help, i think that will work for now...

Thx for your time =)

Best regards
Australia Forum Administrator #5
Fiendish said:



local result = {}
local name = string.lower(%1)




You need to quote the %1 there (and further down), otherwise it expands to:


local name = string.lower(Mike)


... which is referring to the non-existent variable Mike.

It should read:


local result = {}
local name = string.lower("%1")


Ditto for lower down where it displays the results.


print("There are " .. #result .." %1 in your table:")


It would also help to make a "plain" search in case you happened to search for (say) ^Mike, so it could read:


if string.find (v:lower (), name, 1, true) then


USA Global Moderator #6
Heh. I suppose I should have actually tried it out first. :)