Sorting a Table Containing Tables - sorting

I can sort a table with two pieces of information (the name and a second piece, such as age) with the following code:
t = {
Steve = 4,
Derek = 1,
Mike = 5,
Steph = 10,
Mary = 7,
Danny = 2
}
function pairsByKeys (t,f)
local a = {}
for x in pairs (t) do
a[#a + 1] = x
end
table.sort(a,f)
local i = 0
return function ()
i = i + 1
return a[i], t[a[i]]
end
end
for a,t in pairsByKeys (t) do
print (a,t)
end
Result:
Danny 2
Derek 1
Mary 7
Mike 5
Steph 10
Steve 4
I have a scenario where at a convention each person's name tag contains a barcode. This barcode, when scanned, enters four pieces of information about each person into a table database. This database is made up of the following pieces:
t = {
{name = "Mike", addr = "738 Rose Rd", age = 30, phone = "333-902-6543"}
{name = "Steph", addr = "1010 Mustang Dr", age = 28, phone = "555-842-0606"}
{name = "George", addr = "211 Glass St", age = 34, phone = "111-294-9903"}
}
But how would I change my code to sort all four entries (name, addr, age, phone) by age and keep all variables in line with one another?
I've been trying to experiment and am getting the hang of sorting a table by pairs and have a better idea how to perform table.sort. But now I want to take this another step.
Can I please receive some help from one of the programming gurus here?! It is greatly appreciated guys! Thanks!

You could use the age as the key of your table:
t = {
[30] = {name = "Mike", addr = "738 Rose Rd", age = 30, phone = "333-902-6543"},
[28] = {name = "Steph", addr = "1010 Mustang Dr", age = 28, phone = "555-842-0606"},
[34] = {name = "George", addr = "211 Glass St", age = 34, phone = "111-294-9903"},
}
function pairsByKeys (t,f)
local a = {}
for x in pairs (t) do
a[#a + 1] = x
end
table.sort(a,f)
local i = 0
return function ()
i = i + 1
return a[i], t[a[i]]
end
end
for a,t in pairsByKeys (t) do
print (t.name, t.addr, t.age, t.phone)
end
EDIT
Otherwise, if you don't want to change the structure of t, you could change your iterator generating function to keep track of the indexing:
t = {
{name = "Mike", addr = "738 Rose Rd", age = 30, phone = "333-902-6543"},
{name = "Steph", addr = "1010 Mustang Dr", age = 28, phone = "555-842-0606"},
{name = "George", addr = "211 Glass St", age = 34, phone = "111-294-9903"},
}
function pairsByAgeField(t,f)
local a = {}
local index = {}
for _, x in pairs(t) do
local age = x.age
a[#a + 1] = age
index[age] = x
end
table.sort(a,f)
local i = 0
return function ()
i = i + 1
return a[i], index[a[i]]
end
end
for a,t in pairsByAgeField(t) do
print (t.name, t.addr, t.age, t.phone)
end
Of course this makes pairsByAgeField less generally applicable than your original pairsByKeys (it assumes that the table being iterated has a given structure), but this is not a problem if you often have to deal with tables such as t in your application.

Related

Pinescript //#version=5, trying to combine 2 EMAs and PSAR and add buy/sell signals

im really new at coding, so i know it doesnt run, but this what i got.
I want to show buy signal when PSAR goes below the chart and the EMAs are below the chart(EMA200 below EMA20)
I want to show sellsignal when PSAR goes above the chart and the EMAs are above the chart(EMA200 above EMA20)
I find it really good automated startegy with R.R=2 .
if you can help me run the code i reall appreciate your time and effort.
my effort is below - THANKS AGAIN :
strategy("PSAR 2EMAs Strategy",shorttitle="PSAR Buy/Sell" ,overlay=true )
indicator(title="Parabolic SAR", shorttitle="SAR", overlay=true, timeframe="",
timeframe_gaps=true)
start = input(0.02)
increment = input(0.02)
maximum = input(0.2, "Max Value")
out3 = ta.sar(start, increment, maximum)
plot(out3, "ParabolicSAR", style=plot.style_cross, color=#2962FF)
indicator(title="Moving Average Exponential", shorttitle="EMA1", overlay=true,
timeframe="", timeframe_gaps=true)
len = input.int(20, minval=1, title="Length")
src = input(close, title="Source")
offset = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out = ta.ema(src, len)
plot(out, title="EMA", color=color.yellow, offset=offset)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA
(RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100,
group="Smoothing")
smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset,
display=display.none)
indicator(title="Moving Average Exponential", shorttitle="EMA2", overlay=true,
timeframe="", timeframe_gaps=true)
len2 = input.int(200, minval=1, title="Length")
src2 = input(close, title="Source")
offset2 = input.int(title="Offset", defval=0, minval=-500, maxval=500)
out2 = ta.ema(src, len)
plot(out2, title="EMA", color=color.blue, offset2=offset2)
ma(source, length, type) =>
switch type
"SMA" => ta.sma(source, length)
"EMA" => ta.ema(source, length)
"SMMA (RMA)" => ta.rma(source, length)
"WMA" => ta.wma(source, length)
"VWMA" => ta.vwma(source, length)
typeMA = input.string(title = "Method", defval = "SMA", options=["SMA", "EMA", "SMMA
(RMA)", "WMA", "VWMA"], group="Smoothing")
smoothingLength = input.int(title = "Length", defval = 5, minval = 1, maxval = 100,
group="Smoothing")
smoothingLine = ma(out, smoothingLength, typeMA)
plot(smoothingLine, title="Smoothing Line", color=#f37f20, offset=offset,
display=display.none)
//the strategy goes like this: WHEN PSAR gets below the chart and EMA200 is below EMA20
and chart is above the 2 EMAs: "BUY"
// WHEN PSAR gets above the chart and EMA200 is above EMA20
and chart is below the 2 EMAs: "SELL"
if ta.change("PSAR Buy/Sell") < 0 and EMA1 below EMA2
strategy.entry("My Long Entry Id", strategy.long)
if ta.change("PSAR Buy/Sell") > 0 and EMA1 above EMA2
strategy.entry("My Short Entry Id", strategy.short)

Stuck filling an array using VBscript and a For Loop

I have created an array to store some excel cell addresses, but while running through a portion of the array with a For Loop (because the values are in order for that portion of the data and its easier than doing each one at a time), but I get an error at line 22,(saData(i,1) = "D" count), right at the "count" variable but I cant figure out why.
'LOADING EXCEL CELL ADDRESS INTO INPUT ARRAY
saData(0,1) = "C3"
saData(1,1) = "F3"
saData(2,1) = "C4"
saData(3,1) = "F4"
saData(4,1) = "F6"
saData(5,1) = "C7"
saData(6,1) = "F7"
saData(7,1) = "C8"
saData(8,1) = "C9"
saData(9,1) = "F9"
saData(10,1) = "C11"
saData(12,1) = "F11"
saData(13,1) = "C13"
saData(14,1) = "F13"
Dim count : count = 16
For i = 15 to 54
saData(i,1) = "D" count
count = count + 1
next'
saData(55,1) = "G59"
saData(56,1) = "F60"
saData(57,1) = "B64"
saData(58,1) = "E64"
For i = 0 to 59
Msgbox saData(i,1)
next
Just try concatenating saData(i,1) = "D"&count

Debugging generics in Lazarus IDE

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}

how to sort a table in lua?

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

R: tm Textmining package: Doc-Level metadata generation is slow

I have a list of documents to process, and for each record I want to attach some metadata to the document "member" inside the "corpus" data structure that tm, the R package, generates (from reading in text files).
This for-loop works but it is very slow,
Performance seems to degrade as a function f ~ 1/n_docs.
for (i in seq(from= 1, to=length(corpus), by=1)){
if(opts$options$verbose == TRUE || i %% 50 == 0){
print(paste(i, " ", substr(corpus[[i]], 1, 140), sep = " "))
}
DublinCore(corpus[[i]], "title") = csv[[i,10]]
DublinCore(corpus[[i]], "Publisher" ) = csv[[i,16]] #institutions
}
This may do something to the corpus variable but I don't know what.
But when I put it inside a tm_map() (similar to lapply() function), it runs much faster, but the changes are not made persistent:
i = 0
corpus = tm_map(corpus, function(x){
i <<- i + 1
if(opts$options$verbose == TRUE){
print(paste(i, " ", substr(x, 1, 140), sep = " "))
}
meta(x, tag = "Heading") = csv[[i,10]]
meta(x, tag = "publisher" ) = csv[[i,16]]
})
Variable corpus has empty metadata fields after exiting the tm_map function. It should be filled. I have a few other things to do with the collection.
The R documentation for the meta() function says this:
Examples:
data("crude")
meta(crude[[1]])
DublinCore(crude[[1]])
meta(crude[[1]], tag = "Topics")
meta(crude[[1]], tag = "Comment") <- "A short comment."
meta(crude[[1]], tag = "Topics") <- NULL
DublinCore(crude[[1]], tag = "creator") <- "Ano Nymous"
DublinCore(crude[[1]], tag = "Format") <- "XML"
DublinCore(crude[[1]])
meta(crude[[1]])
meta(crude)
meta(crude, type = "corpus")
meta(crude, "labels") <- 21:40
meta(crude)
I tried many of these calls (with var "corpus" instead of "crude"), but they do not seem to work.
Someone else once seemed to have had the same problem with a similar data set (forum post from 2009, no response)
Here's a bit of benchmarking...
With the for loop :
expr.for <- function() {
for (i in seq(from= 1, to=length(corpus), by=1)){
DublinCore(corpus[[i]], "title") = LETTERS[round(runif(26))]
DublinCore(corpus[[i]], "Publisher" ) = LETTERS[round(runif(26))]
}
}
microbenchmark(expr.for())
# Unit: milliseconds
# expr min lq median uq max
# 1 expr.for() 21.50504 22.40111 23.56246 23.90446 70.12398
With tm_map :
corpus <- crude
expr.map <- function() {
tm_map(corpus, function(x) {
meta(x, "title") = LETTERS[round(runif(26))]
meta(x, "Publisher" ) = LETTERS[round(runif(26))]
x
})
}
microbenchmark(expr.map())
# Unit: milliseconds
# expr min lq median uq max
# 1 expr.map() 5.575842 5.700616 5.796284 5.886589 8.753482
So the tm_map version, as you noticed, seems to be about 4 times faster.
In your question you say that the changes in the tm_map version are not persistent, it is because you don't return x at the end of your anonymous function. In the end it should be :
meta(x, tag = "Heading") = csv[[i,10]]
meta(x, tag = "publisher" ) = csv[[i,16]]
x

Resources