 |
Cheat Engine The Official Site of Cheat Engine
|
| View previous topic :: View next topic |
| Author |
Message |
MikeNoey Advanced Cheater
Reputation: 0
Joined: 08 Jun 2018 Posts: 64
|
Posted: Sun May 19, 2019 5:27 am Post subject: Multiple string values for one reference ? |
|
|
I'm just wondering if it's possible to assign more than a single value to a reference. So something like this
Reference = 'Yellow' , 'Green' , 'Blue' , 'Orange' , 'Purple'
if (StringAdd ~= Reference) then
print "Not a color"
elseif (StringAdd == Reference) then
print "Is a color"
end
Appreciate the assistance.
|
|
| Back to top |
|
 |
TheyCallMeTim13 Wiki Contributor
Reputation: 51
Joined: 24 Feb 2017 Posts: 976 Location: Pluto
|
Posted: Sun May 19, 2019 6:19 am Post subject: |
|
|
| Code: | function table.contains(t, element)
for _, value in pairs(t) do
if value == element then
return true
end
end
return false
end
References = { 'Yellow' , 'Green' , 'Blue' , 'Orange' , 'Purple' }
if table.contains(References, StringAdd) then
print('Is in list.')
else
print('Is not in list.')
end |
_________________
|
|
| Back to top |
|
 |
Corroder Grandmaster Cheater Supreme
Reputation: 75
Joined: 10 Apr 2015 Posts: 1668
|
Posted: Sun May 19, 2019 6:59 am Post subject: |
|
|
or using a string list:
| Code: | list = createStringlist()
list.Sorted=true
list.add('Yellow')
list.add('Green')
list.add('Blue')
list.add('Orange')
list.add('Purple')
--print(list.Text )
function add2List(sl, color)
for i = 0,sl.Count-1 do
if sl[i] == color then
print(color..' has on the list')
else
list.add(color)
end
end
end
add2List(list,'Orange') -- Orange has on the list
|
Class:Stringlist also have poperties:
| Code: | | setDuplicates() : Sets the duplicates property (dupIgnore, dupAccept, dupError) |
So, when stringlist set duplicate, for example:
| Code: | | list.set duplicate(dupError) |
Will return an error message when trying to add an item already has on the string list.
To use setDuplicate, a string list must be sorted to get it to work.
EDIT:
TO use TheyCallMeTim function, it should be using 'ipairs' if the reference in no particular order.
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
| Back to top |
|
 |
MikeNoey Advanced Cheater
Reputation: 0
Joined: 08 Jun 2018 Posts: 64
|
Posted: Mon May 20, 2019 12:40 am Post subject: |
|
|
Thank you guys for the help.
I don't quite understand what you mean about Ipairs @ Corroder
I tried changing the order and it still works with "pairs". Could you maybe show me an example ?
|
|
| Back to top |
|
 |
Corroder Grandmaster Cheater Supreme
Reputation: 75
Joined: 10 Apr 2015 Posts: 1668
|
Posted: Mon May 20, 2019 2:02 am Post subject: |
|
|
For your 'reference' structure now, it will work both pairs or ipairs.
Example :
A Lua Table with flat structure in particular order without key like your 'reference' table.
| Code: | | ref = {'Green', 'Blue', 'Red', 'Orange'} |
For this table above, there are no keys. Both pairs or ipairs, should work.
| Code: | --- print the contains element
-- #1
print(table.concat(ref,", "))
-- result = Green, Blue, Red, Orange
-- or
for k, v in pairs(ref) do
print(v)
end
------------------ result =
-- Green
-- Blue
-- Red
-- Orange
for k, v in ipairs(ref) do
print(v)
end
---------------- result =
-- Green
-- Blue
-- Red
-- Orange
|
--#2
Now, let said we have a table with multi array dimensions, for example:
| Code: | local ref = {
{
name = "Fred",
address = "16 Long Street",
phone = "123456"
},
{
name = "Wilma",
address = "16 Long Street",
phone = "123456"
},
{
name = "Barney",
address = "17 Long Street",
phone = "123457"
}
}
for k, v in pairs(ref) do
print(v)
end
--- result = Nothing
for key, value in pairs(ref) do
print(key, value)
end
--- result = 1 2 3 (the index of element)
for index, data in ipairs(ref) do
print(index)
for key, value in pairs(data) do
print('\t', key, value) --- \t to make indent
end
end
result :[[
1
phone 123456
address 16 Long Street
name Fred
2
phone 123456
address 16 Long Street
name Wilma
3
phone 123457
address 17 Long Street
name Barney
]] |
To display nested tables you will have to use nested loops.
Also, use ipairs to iterate through array-like tables, and pairs to iterate through record-like tables.
For more info:
https://www.lua.org/manual/5.3/manual.html#pdf-ipairs
https://www.lua.org/manual/5.3/manual.html#pdf-pairs
http://www.luafaq.org/#T1.10
https://www.youtube.com/watch?v=iaJOA47qYGE
In short, pairs use for unordered table and ipairs use for ordered table.
_________________
Stealing Code From Stolen Code...
And Admit It.. Hmmm....Typically LOL |
|
| Back to top |
|
 |
MikeNoey Advanced Cheater
Reputation: 0
Joined: 08 Jun 2018 Posts: 64
|
Posted: Tue May 28, 2019 10:07 am Post subject: |
|
|
how would I make this work ? When using 2 string values instead of 1
function table.contains(t, element)
for _, value in pairs(t) do
if value == element then
return true
end
end
return false
end
Time = 'Yellow'
Zone = 'Black'
References = { 'Yellow Black' , 'Green' , 'Blue' , 'Orange' , 'Purple' }
if table.contains (References, Time,Zone) then
print('Is in list.')
else
print('Is not in list.')
end
The result I get is "Is not in list"
|
|
| Back to top |
|
 |
ParkourPenguin I post too much
Reputation: 152
Joined: 06 Jul 2014 Posts: 4706
|
Posted: Tue May 28, 2019 11:22 am Post subject: |
|
|
| Code: | function contains(table, ...)
for _,q in ipairs({...}) do
local found = false
for _,v in pairs(table) do
if q == v then found=true; break; end
end
if not found then return false end
end
return true
end |
_________________
I don't know where I'm going, but I'll figure it out when I get there. |
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum You cannot attach files in this forum You can download files in this forum
|
|