Table problems

Post Reply
azure_glass
Posts: 97
Joined: Wed Jul 25, 2012 12:35 pm

Table problems

Post by azure_glass »

Hi i want to do simple table with room positions in it

How can I look for previous RoomID?

t = {1212, 1111, 4444, 5555}

last RoomID was 5555 so how to extract 4444 ?

And if table look like:

t = {2222, 5555, 4444, 5555}

there won't be bug like lastRoomID will matches as 2222 (should be 4444) because script matches first 5555 in table?


And second question: I have 2 tables with i want connect to table "enemies" but what i have tried it displays an empty table

elves = {
{desc = "high elf", name ="bob"},
{desc = "wood elf", name ="Tree"},
}

ogres= {
{desc = "pink ogre", name ="pinky"},
{desc = "blue ogre", name ="brain"},
}

And last question how to make alias for searching tables

/find pinky

desc = "pink ogre", name ="pinky", guild = "ogres"
English is not my native language. If you don't understand what im writing ask. :)
Ubuntu 17.04, Mudlet 3.1

User avatar
Akaya
Posts: 414
Joined: Thu Apr 19, 2012 1:36 am

Re: Table problems

Post by Akaya »

Didn't quite understand some of that, but hopefully this will help:
azure_glass wrote: How can I look for previous RoomID?
Code: [show] | [select all] lua
if table.contains(t,4444) then
  do_stuff()
end
azure_glass wrote: I have 2 tables with i want connect to table "enemies" but what i have tried it displays an empty table
You can do something like this for your table:
Code: [show] | [select all] lua
elves = {
  [1] = {
    desc = "high elf",
    name = "bob",
  },
  [2] = {
    desc = "wood elf",
    name = "Tree",
  }
}

ogres = {
  [1] = {
    desc = "pink ogre", 
    name ="pinky"
  },
  [2] = {
    desc = "blue ogre", 
    name ="brain"
  }
}
With the table above, values are stored like so: elves[1].desc which would return "high elf"

So say you want an alias to list every ogre name, you could:
Code: [show] | [select all] lua
--Alias Pattern: list ogre
for k,v in pairs(ogres) do
  display(v.name)
end
Hope that helps!

azure_glass
Posts: 97
Joined: Wed Jul 25, 2012 12:35 pm

Re: Table problems

Post by azure_glass »

Thanks for response.
I need to improve my English writhing skill...

About searching previous roomID i mean:

Long table with i don't know how big is.
And every visited room adds another value.

The problem is in that i want to search previous value to last added value

Another question if table contains x > 1000000 values
that affect on speed of script?
Do i have to clear this table?
English is not my native language. If you don't understand what im writing ask. :)
Ubuntu 17.04, Mudlet 3.1

User avatar
Oneymus
Posts: 321
Joined: Thu Sep 17, 2009 5:24 am

Re: Table problems

Post by Oneymus »

In order to see the last element of a table, you would index into it like so:
Code: [show] | [select all] lua
local lastValue = tbl[#tbl]
Therefore, you can easily see that in order to get the second-to-last value, you should do this:
Code: [show] | [select all] lua
local secondToLastValue = tbl[#tbl - 1]
The # symbol gets you the length of a table. Because Lua tables begin with index 1 (as opposed to 0), the last element in the table is at the index equal to the length of the table.

This, of course, only works with numerically indexed tables. Key-value tables have no order, and so you cannot find the last element.

As for searching, this is a complex topic, and is often implementation specific, as it is in your case. I can provide an implementation, though this is a simplistic solution.

First, your alias:
Code: [show] | [select all] lua
-- Pattern: ^find (.+)$

-- Code:
-- This kind of search will always be case insensitive. I leave
-- it as an exercise for you to develop a case sensitive version,
-- though the function code is already in place.
local resultTable = findTextIn(tableToSearch, matches[2])
if resultTable then
    -- You'll probably want to do more than just display it.
    display(resultTable)
end
And now, your code:
Code: [show] | [select all] lua
function findTextIn (tableToSearch, textToFind, caseSensitive)
    for _, subTable in ipairs(tableToSearch) do
        -- This may not be necessary if you can guarantee correct table
        -- structure.
        if type(subTable) ~= "table" then
            continue
        end

        for field, value in pairs(subTable) do
            -- Can't find text in a number, unless you're into that sort of
            -- thing.
            if type(value) ~= "string" then
                continue
            end

            -- If this is a case insensitive search, lowercase everything
            -- to make the search more inclusive.
            local textToSearch = value
            if not caseSensitive then
                textToSearch = textToSearch:lower()
                textToFind = textToFind:lower()
            end

            -- string.find() returns nil if it does not find the text, or
            -- or a number indicating the beginning of the string.
            if textToSearch:find(textToFind) then
                -- The assumption here is that you want the table that
                -- contains the data. Also, returns the first positive
                -- result it comes across, as opposed to all results.
                return subTable
            end
        end
    end
end
As always, this comes with a disclaimer: this code was not tested. I typed it into the forums directly. If it doesn't work, check the error window and try to fix it yourself first. This may not solve your problem, given it was written based on the limited information provided. However, this should easily serve as a starting point.

phasma
Posts: 191
Joined: Sat Aug 03, 2013 7:00 pm
Discord: phasma#4694

Re: Table problems

Post by phasma »

Nice reply, though if I may add, # can also be used to determine the length of other types, most notably strings. It's really handy.

azure_glass
Posts: 97
Joined: Wed Jul 25, 2012 12:35 pm

Re: Table problems

Post by azure_glass »

Thanks for your replies ;)

I have found some code on forum and i have something like this for sorting my guild chest;

Code: Select all

tab = matches[5]
tab = string.split(tab, ",")
--display(t)

deleteLine()
echo("\n")
   local inc = math.ceil(#tab*0.5)
   for i = 1,inc do
      local val1 = tab[i];
      local val2 = tab[i + inc];

      if( not val1 ) then val1 = ""; end;
      if( not val2 ) then val2 = ""; end;

      print(" |", val1.." \t| ", val2.." |");
   end

display(#tab)
It's causing huge lags and "|" is not coretly positioned if item has longer/shorter name.

How to improve it?
- no lags
- adding replace like: "two" swords to "[ 2]" swords
- and propper columns.
- sometimes color sword if magic

Text for testing:

Code: Select all

Open wooden chest contains cztery nabijane czernione morgensterny, kosciana jednoreczna maczuge, masywna kolczasta wekiere, cztery drewniane duze maczugi, piec lekkich dlugich morgensternow, ciezka wymalowana zbroje plytowa, dwie polowe stalowe zbroje plytowe, siedem czarnych plomienistych sztyletow, solidny obreczowy helm, szesc czarnych luskowych kaftanow z okuciami, trzy tileanskie sprezynowe stiletta, cztery dlugie krwiste sztylety, dwa czernione kunsztowne napiersniki, wyprofilowany mocny napiersnik, czarny lsniacy helm, wiele posrebrzanych zdobionych sztyletow, polkulisty czarny helm z kita, szmaragdowosrebrna luskowa pare rekawic, szesc ciemnoszarych mocnych naramiennikow, dwa wytrzymale zdobione bigwanty, cztery mocne spizowe kirysy, piec dlugich szablastych naginat, kawaleryjski lyrijski nadziak, trzy zlociste luskowe karaceny, ciezka luskowa karacene, trzy poltorareczne zdobione miecze, smukly srebrzysty miecz, czarny obsydianowy miecz, szesc posrebrzanych szerokich katzbalgerow, szeroka ogromna partyzane, siedem masywnych brunirowanych nareczakow, brazowa wzmocniona pare butow, stalowa ciezka pare nareczakow, trzy lekkie stalowe pary naramiennikow, srebrzyste stalowe naramienniki, trzy kolczaste stalowe pary naramiennikow, czarna pare naramiennikow, dwa karbowane prawe naramienniki, srebrzysty lewy naramiennik, hematytowy smukly prawy naramiennik, lazurowy smukly lewy naramiennik, ciezki czarny lewy naramiennik, dwa srebrzyste prawe naramienniki, karbowany lewy naramiennik, drewniana okuta tarcze, okragla zdobiona tarcze, prostokatna metalowa tarcze, migdalowata solidna tarcze, drewniana poreczna tarcze, dwa ciezkie spizowe puklerze, dwa duze opancerzone fragmenty szkieletu, ciemny rawerowany kirys, szkarlatny lekki kirys, czarny duzy morion, spizowy solidny basinet, polyskliwy czarny helm, cztery mocne spizowe helmy, trzy czarne rogate helmy, stalowy wysoki basinet, dwa stalowe spiczaste szyszaki, czubiasty zdobny helm, dwa czarne luskowe helmy, dwie zdobione azurowe partyzany, pierscieniowy twardy kaftan, lazurowy wytrzymaly kaftan, blyszczaca zbroje plytowa, srebrzysta kolcza pare rekawic, szesc kolczastych plytowych par rekawic, pare wytrzymalych skorzanych rekawic, siedem skorzanych lsniacych par rekawic, lekki zdobiony polpancerz, dziewiec czarnych skorzanych kaftanow, antracytowa ornamentowana pare nareczakow, szara chropowata karacene, stalowa pare nareczakow, grube skorzane rekawice, plytowa dluga pare rekawic, dwie srebrzyste krotkie wekiery, szesc bojowych okutych cepow, zmurszala trojdzielna korseke, dwa dlugie bojowe nadziaki, dwa bursztynowe plomieniste sztylety, dwa zakrzywione krotkie nozyki, plomienisty lsniacy flamberg, dwa srebrzyste polyskliwe miecze, polyskliwy zakrzywiony scimitar, smukly trojkatny miecz, ciezka dluga glewie, dluga prosta gizarme, dziewiec srebrnych lsniacych toporow, czarna zdobiona halabarde, dluga czarna halabarde, polyskliwa bojowa halabarde, szmaragdowa ostra spise, dwa zielonkawe bretonskie mloty, ciezki zdobiony topor, waska trojdzielna spise, szesc ciezkich jednorecznych toporow, wysluzona dluga pike, polyskliwy runiczny mlot bojowy, olbrzymi spizowy mlot bojowy, krotki masywny mlot, trzy ornamentowane bojowe mloty, dwureczny gnomi mlot, dwureczny czarny mlot, trzy metalowe krotkie nadziaki, siedem krotkich lekkich nadziakow, dwa masywne bitewne mloty, dwa poreczne mahakamskie mloty wojenne, dwa zdobione srebrzyste obuszki, dwa srebrzyste jednoreczne topory, dwa poreczne polkoliste topory bojowe i dwie czarne rekawiczki.
English is not my native language. If you don't understand what im writing ask. :)
Ubuntu 17.04, Mudlet 3.1

Post Reply