I using linq.js and i want replace single quotes when searching data.
This my code.
var list = [
{ a: "50", b: 4, c: 1 },
{ a: "60", b: 3, c: 7 },
{ a: "'540'", b: 3, c: 3 }
];
var val = "'540'";
val = val.replace(/'/g, "'");
var res = Enumerable.From(list).Where("($.a).replace(\"'\",\"'\")=='" + val + "'").ToArray();
If there is only 1 single quote in the data, it works.
But if there are 2 single quote in it, it can't search.
Oh, linq.js the same code in javascript.
I have to use regex to replace:
It is ok , if i change to :
var res = Enumerable.From(list).Where("($.a).replace(\/'\/g,\"'\")=='" + val + "'").ToArray();
Related
I have a table that looks like this
{
["slot1"] = {}
["slot2"] = {}
["slot3"] = {}
["slot4"] = {}
["slot5"] = {}
["slot6"] = {}
}
When I do a for k, v in pairs loop I want the keys to go from slot1- the last slot. At the moment when I do a loop the order is inconsistent, slot 5 comes first etc. What is the best way to do this?
also I did not design this table, and I cannot change how the keys look
You can write a simple custom iterator:
local tbl = {
["slot1"] = {},
["slot2"] = {},
["slot3"] = {},
["slot4"] = {},
["slot5"] = {},
["slot6"] = {}
}
function slots(tbl)
local i = 0
return function()
i = i + 1
if tbl["slot" .. i] ~= nil then
return i, tbl["slot" .. i]
end
end
end
for i, element in slots(tbl) do
print(i, element)
end
Output:
1 table: 0xd575f0
2 table: 0xd57720
3 table: 0xd57760
4 table: 0xd5aa40
5 table: 0xd5aa80
6 table: 0xd5aac0
Create a new table:
slot = {}
for k,v in pairs(original_table) do
local i=tonumber(k:match("%d+$"))
slot[i]=v
end
Lua table orders are undeterministic. see here what makes lua tables key order be undeterministic
For your table you can try this
local t = {
["slot1"] = {},
["slot2"] = {},
["slot3"] = {},
["slot4"] = {},
["slot5"] = {},
["slot6"] = {}
}
local slotNumber = 1
while(t['slot' .. slotNumber]) do
slot = t['slot' .. slotNumber]
-- do stuff with slot
slotNumber = slotNumber + 1
end
This method does not handle if the table skips a slot number.
I have this:
TDictionaryStrInt = specialize TFPGMap<string, integer>;
Can somebody tell me how the heck can I debug the Map, the Key/Value pairs?
I just see only reference to a memory address, but I really need to see the items.
Watch, local variables does not helps me.
I can see only this:
<TDictionaryStrStr> = {
<TFPSMAP> = {
<TFPSLIST> = {
<TOBJECT> = {
_vptr$ = {
0x5612ec,
0x230b988}},
FLIST = ,
FCOUNT = 1,
FCAPACITY = 4,
FITEMSIZE = 8},
FKEYSIZE = 4,
FDATASIZE = 4,
FDUPLICATES = DUPIGNORE,
FSORTED = false,
FONKEYPTRCOMPARE = $426b70 <TFPGMAP$2$CRC36DB32B4__KEYCOMPARE>,
FONDATAPTRCOMPARE = $523e30
<FGL$_$TFPSMAP_$__$$_BINARYCOMPAREDATA$POINTER$POINTER$$LONGINT>},
FONKEYCOMPARE = $0,
FONDATACOMPARE = $0}
I'm looking for a method of sorting a Lua table by its values chain. Say, the table:
local vals = {
{ id = "checkpoint4" },
{ id = "checkpoint1", nextid = "checkpoint2" },
{ id = "checkpoint3", nextid = "checkpoint4" },
{ id = "checkpoint2", nextid = "checkpoint3" },
}
Should transform into this after sorting:
local vals = {
{ id = "checkpoint1", nextid = "checkpoint2" },
{ id = "checkpoint2", nextid = "checkpoint3" },
{ id = "checkpoint3", nextid = "checkpoint4" },
{ id = "checkpoint4" },
}
It's not essentially with the exact same names, they might vary. I wanted to make the comparison of numbers after "checkpoint", but it turned out that I have to work with dynamic things like this (already sorted the way I want it to be):
local vals = {
{ id = "checkpoint1", nextid = "cp" },
{ id = "cp", nextid = "chp" },
{ id = "chp", nextid = "mynextcheckpoint" },
{ id = "mynextcheckpoint"},
}
Thanks.
What you are describing is called topological sorting. However, since this is a restricted case, we do not have to implement a complete topological sorting algorithm:
function sort_list(tbl)
local preceding = {}
local ending
local sorted = {}
for i, e in ipairs(tbl) do
if e.nextid == nil then
ending = e
else
preceding[e.nextid] = i
end
end
if ending == nil then
return nil, "no ending"
end
local j = #tbl
while ending ~= nil do
sorted[j] = ending
ending = tbl[preceding[ending.id]]
j = j - 1
end
if sorted[1] == nil then
return nil, "incomplete list"
end
return sorted
end
Usage:
local sorted = sort_list(vals)
local id2val, tailsizes = {}, {}
for _, val in ipairs(vals) do id2val[val.id] = val end
local function tailsize(val) -- memoized calculation of tails sizes
if not tailsizes[val] then
tailsizes[val] = 0 -- cycle-proof
if val.nextid and id2val[val.nextid] then -- dangling nextid proof
tailsizes[val] = tailsize(id2val[val.nextid]) + 1
end
end
return tailsizes[val]
end
-- sorting according to tails sizes
table.sort(vals, function(a,b) return tailsize(a) > tailsize(b) end)
I have a lua table that contains 2 key pieces of data. I would like to sort the table in ascending order by the "num1" column, or if thats not possible, they by the key value in ascending order
Here's what I have so far:
local widgets = {}
widgets[1568] = {}
widgets[1568]["num1"] = 99999
widgets[1568]["val2"] = "NA"
widgets[246] = {}
widgets[246]["num1"] = 90885
widgets[246]["val2"] = "NA"
widgets[250] = {}
widgets[250]["num1"] = 95689
widgets[250]["val2"] = "NA"
widgets[251] = {}
widgets[251]["num1"] = 95326
widgets[251]["val2"] = "NA"
widgets[252] = {}
widgets[252]["num1"] = 95301
widgets[252]["val2"] = "NA"
widgets[256] = {}
widgets[256]["num1"] = 95303
widgets[256]["val2"] = "NA"
-- ATTEMPT TO SORT
--table.sort(widgets, function(a,b) return tonumber(a.num1.value) < tonumber(b.num1.value) end)
--table.sort(widgets, function(a,b) return tonumber(a.num1) < tonumber(b.num1) end)
--TRY SORTING BY ID:
table.sort(widgets, function(a,b) return tonumber(a) < tonumber(b) end)
for i, v in pairs(widgets) do
print(v.num1)
end
Any suggestions would be appreciated. Right now, I'm reviewing Sort a Table[] in Lua to try to understand the "spairs" function. But that example is slightly different because I have a table within a table...
Thanks.
SOLUTION
In line with the answer below, I created a new table and added the records from the old table, one by one, using table insert like so:
local new_widgets = {}
for i, v in pairs(widgets) do
table.insert(new_widgets, id=v.id, num1= v.num1, num2 = v.num2)
end
then I sorted new_wigets.
Lua tables are hashtables. Their entries have no specific order.
You fake it by using consecutive numerical indices then iterating by incrementing a number (note: internally Lua actually will implement this as an array, but that's an implementation detail; conceptually, table entries have no specific order).
t[2] = "two"
t[3] = "three"
t[1] = "one"
for i=1,#t do print(t[i]) end
ipairs creates an iterator that does the same thing as this for loop.
So, if you want your data sorted, you need to put it in a table with consecutive numeric indices.
In your case, there's a lot of different ways you can do it. Here's one way to skin that cat:
Instead of this:
local widgets = {
[246] = { num1 = 90885, val2 = "NA" }
[250] = { num1 = 95689, val2 = "NA" }
[251] = { num1 = 95326, val2 = "NA" }
[252] = { num1 = 95301, val2 = "NA" }
[256] = { num1 = 95303, val2 = "NA" }
}
You want this:
local widgets = {
{ id = 246, num1 = 90885, val2 = "NA" },
{ id = 250, num1 = 95689, val2 = "NA" },
{ id = 251, num1 = 95326, val2 = "NA" },
{ id = 252, num1 = 95301, val2 = "NA" },
{ id = 256, num1 = 95303, val2 = "NA" },
}
-- sort ascending by num1
table.sort(widgets, function(a,b) return a.num1 < b.num1 end)
for i, widget in ipairs(widgets) do
print(widget.num1)
end
If you need the ability to then lookup a widget quickly by id, you can create a lookup table for that:
local widgetById = {}
for i,widget in pairs(widgets) do
widgetById[widget.id] = widget
end
I have a column type int and I want to check the value to display string instead of the number.
I added this expression
=IIf(Fields!Categ.Value = 1, "First", "")
=IIf(Fields!Categ.Value = 2, "Second", "")
=IIf(Fields!Categ.Value = 3, "Third", "")
=IIf(Fields!Categ.Value = 4, "Fourth", "")
=IIf(Fields!Categ.Value = 5, "Fifth", "")
but it is not working.
How can I do it?
Can you try this?
=Switch(
Fields!Categ.Value = 1, "First",
Fields!Categ.Value = 2, "Second",
Fields!Categ.Value = 3, "Third",
Fields!Categ.Value = 4, "Fourth",
Fields!Categ.Value = 5, "Fifth",
Fields!Categ.Value > 5, "",
Fields!Categ.Value < 1, "",
)
I believe the values returned by the .value condition are strings so you could either have to cast them or do a string comparison.
String Comparison
=IIf(Fields!Categ.Value == "1", "First", "")
Int Cast
=IIf(Int.Parse(Fields!Categ.Value) == 1, "First", "")
Its been awhile since I have worked with RDLC.
To add embedded code to a report, use the Code tab of the Report Properties dialog box. The code block you create can contain multiple methods. Methods in embedded code must be written in Microsoft Visual Basic and must be instance-based.
so for example:
public function getDescription(cat as integer) as string
dim sRet as string
select case cat
case 1: sRet = "first"
case 2: sRet = "second"
case else
sRet = "uh?"
end select
return sRet
end function
then use the expression:
=code.getDescription(fields!Categ.value)