How I can check table for string.find?
My code:
WEAPON_TABLE = { -- I need check this weaponclass of sweps
"swep_1",
"swep_2",
"swep_3"
}
if string.find(v:GetWeaponClass(), ???) then
--
end
Try this:
WEAPON_TABLE = {
["swep_1"]=true,
["swep_2"]=true,
["swep_3"]=true,
}
if WEAPON_TABLE[v:GetWeaponClass()] then
Related
I have this code here that gets a region and add to my variable "regions".
So let' say it has: XYZ, DDA, BBB, ....
Let's say I want to get everything but DDA.
How can I do that.
Code:
def supported_regions(partition)
if ##supported_regions_by_partition_cache[partition].nil?
regions = xrp_supported_regions({ignore_build_status: IGNORE_BUILD_STATUS})
.map { |region| rip_helper.get_region(region) }
.select { |region| region.arn_partition == partition }
.sort_by(&:region_name)
.map(&:airport_code)
##supported_regions_by_partition_cache[partition] = regions
else
regions = ##supported_regions_by_partition_cache[partition]
end
regions
end
I already tried doing:
regions.delete('DDA')
also
.reject {|s| 'DDA' != s }
Not sure how I can do this. I am very new on Ruby.
You can use the select method and replace this line:
FROM
.select { |region| region.arn_partition == partition }
TO
.select { |region| region.arn_partition == partition && region.region_name != "DDA" }
I have a table that looks like this:
{
block_0 = {
hash = "98d1a61c4e3d6394b2970a2a5c44ec2caf172ad5c6844b114867b31fa528220e",
index = 0
}
}
Shouldn't I be able to access the index and hash values of block_0 by saying chain["block_0"]["hash"]? It is not working. When I use this line, I get the error attempt to index a nil value (field 'block_0'). How can I properly access hash and index?
EDIT: Here is some more context:
function add_thing()
block_name = "block_0"
block = { }
block[block_name] = { }
block[block_name]["hash"] = ""
block[block_name]["index"] = ""
block[block_name]["hash"] = "this is a test hash"
block[block_name]["index"] = 10
return block
end
chain = { }
table.insert(chain, add_thing())
require 'pl.pretty'.dump(chain)
You are inserting the return value of add_thing into chain. Thus chain is now a table of tables. To index the correct field you have to index chain first, i.e. chain[1]["block_0"]["hash"]. I rather suspect that this is not the intended behaviour and you want to do the following
local function add_thing(chain)
local block_name = "block_0"
chain[block_name] = {
hash = "this is a test hash",
index = 10
}
end
local chain = {}
add_thing(chain)
print(chain["block_0"]["hash"]) -- this is a test hash
Live on Wandbox
This works as expected because tables are reference types.
Is there a shorter way to do this:
local thisismytable = {
non = sequitur
}
thisismytable.whatismytable = thisismytable
Any help would be appreciated.
I don't want to re-create pre-existing functionality.
No.
If you can stand the difference between these two expressions thisismytable:whatismytable() instead of thisismytable.whatismytable, you could do:
local thisismytable = {
non = sequitur,
whatismytable = function (self) return self end
}
Testing:
print(thisismytable)
print(thisismytable:whatismytable())
More usage:
print(thisismytable:whatismytable().non)
You can't. I use a helper function.
local function ref(t)
for k, v in next, t do
if v == ref then t[k] = t end
end
return t
end
local root = ref{left=ref, right=ref}
assert(root.left == root)
I want to query a list in criteria like this:
def patterns = ["abc%", "cde%"]
def criteria = MyEntity.createCriteria();
def results = criteria.list {
and {
patterns.collect {
not { ilike(name, it) }
}
}
is this basically possible? What do I have to do to get this query working?
Instead of collecting which will create a collection of the contents you need to simply iterate.
def patterns = ["abc%", "cde%"]
def criteria = MyEntity.createCriteria();
def results = criteria.list {
and {
patterns.each {
not { ilike('name', it) }
}
}
If i have this snippet from my action
for (c in AuthorList){
def bookCount = Book.countByName(c)
}
How do i make my bookCount into a list
for loop is not required at all. ;)
authorList.collect { Book.countByName( it ) }
should give the list you are looking for.
Try to use <<
def list = []
for (c in AuthorList){
def bookCount = Book.countByName(c)
list << bookCount
}