Iterate through table using selected keys - lua

The below table and for loop is how we generally access all the key, value pairs in lua
local t = {a1 =11, a2=22, c=3, d=4}
for k, v in pairs(t) do
print(k,v)
end
-- Output: k = a1, a2, c, d & v = 11, 22, 3, 4 respectively
a1 11
a2 22
c 3
d 4
If I want to iterate only on a subset of this table where loop only iterates on certain keys as shown here
k = a1,a2
Since I am intending to add more tables in t as
local t = {
{a1 = 11, a2 = 22, c = 3, d = 4},
{a1 = 12, a2 = 23, c = 2, d = 4},
{a1 = 13, a2 = 24, c = 1, d = 4},
{a1 = 14, a2 = 25, c = 0, d = 4},
{a1 = 15, a2 = 26, c = 0, d = 4}
}
What I want to use something like
for k = {a1, a2} in pairs (t) do
-- something
end
Is there a way to do this other than adding an if condition within a loop, Since this will iterate through all k,v pairs & not desired
for k,v in pairs (t) do
if (k == a1 or k == a2) then
-- something

you can do it this way
local t = {
{a1 = 11, a2 = 22, c = 3, d = 4},
{a1 = 12, a2 = 23, c = 2, d = 4},
{a1 = 13, a2 = 24, c = 1, d = 4},
{a1 = 14, a2 = 25, c = 0, d = 4},
{a1 = 15, a2 = 26, c = 0, d = 4}
}
local keys_to_iterate = {"a1", "a2"}
for index = 1, #t do
for k = 1, #keys_to_iterate do
if t[index][keys_to_iterate[k]] then
print(keys_to_iterate[k] , t[index][keys_to_iterate[k]])
end
end
end
you can see it here
https://repl.it/repls/CoralIndianredVaporware

Related

Is there a way in Dart using the list map method to build a list containing two (or more) items for each source list item?

In Python, this code realizes the objective:
intLst = [1, 2, 3]
f1 = lambda x: x
f2 = lambda x: x * 10
newLst = [f(x) for x in intLst for f in [f1, f2]]
print(newLst) # [1, 10, 2, 20, 3, 30]
but in Dart I was not able to do the same using an anonymous function passed to the map() List method.
You can achieve the same thing using collection for, which allows you to do the same type of things you can do with a list comprehension in python.
void main() {
List<int> intLst = [1, 2, 3];
int Function(int) f1 = (x) => x;
int Function(int) f2 = (x) => x * 10;
List<int> newLst = [
for (var x in intLst)
for (var f in [f1, f2]) f(x),
];
print(newLst); // [1, 10, 2, 20, 3, 30]
}
The alternative would be to use expand rather than map. expand is the same as what some other languages call flatMap.
void main() {
List<int> intLst = [1, 2, 3];
int Function(int) f1 = (x) => x;
int Function(int) f2 = (x) => x * 10;
List<int> newLst = intLst.expand((v) => [f1(v), f2(v)]).toList();
print(newLst); // [1, 10, 2, 20, 3, 30]
}
Here's another way to obtain the same result without using functions:
void main() {
List<int> intList = [1, 2, 3];
List<int> newList = [
for (var x in intList) ...[x, x * 10, x * x],
];
print(newList); // [1, 10, 1, 2, 20, 4, 3, 30, 9]
}

Holt winters Alpha

I am trying to get the HoltWinters Alpha function added.
I have a Table called Sales1 and the code should refer to this table.
Is there anyone who can correct or amend my code below ,so i get the Holtwinters Alpha instead of the Chronbachs Alpha?
Holt winters calc (need this probably amended)
library(forecast)
library(Metrics
)
read_file(sales1)
x <- sales
x = c(Sales1)
mSES = HoltWinters(x, alpha = 0.5, beta = FALSE, gamma = FALSE)
mHW = HoltWinters(x, alpha = 0.5, beta = FALSE, gamma = FALSE)
mSES$SSE
mHW$SSE
HoltWinters(x, alpha = NULL, beta = NULL, gamma = NULL,
seasonal = c("additive", "multiplicative"),
start.periods = 2, l.start = NULL, b.start = NULL,
s.start = NULL,
optim.start = c(alpha = 0.3, beta = 0.1, gamma = 0.1),
optim.control = list())
chronbachs alpha calc
read_file(sales1)
library(tidyverse)
library(psy)
Number of rows before to take into account
rolling = 2
sales1 <- sales::sales( ~date, ~sales,)
#Lag
sales1 = sales1 %>% mutate(lagsales = lag(sales))
#Rolling Chronbachs Alpha.:( I need the Holtwinter Alpha here )
sales1$alpha = c( rep(NA, rolling),
map_dbl((rolling + 1):nrow(sales1), function(x){
cronbach(sales1 %>% select(sales, lagsales) %>% slice((x-rolling):x))$alpha
})
)
sales1
tibbet from Sales1 table:
df <- tibble::tribble(
~seq, ~date, ~sales,
1, "3/01/2017", 40,
2, "4/01/2017", 2,
3, "5/01/2017", 2,
4, "6/01/2017", 2,
5, "7/01/2017", 30,
6, "8/01/2017", 2,
7, "1/02/2017", 9,
8, "2/02/2017", 5,
9, "3/02/2017", 65,
10, "4/02/2017", 3,
11, "5/02/2017", 65

Ruby Intro to Parallel Assignments

a = [1, 2, 3, 4]
b, c = 99, *a → b == 99, c == 1
b, *c = 99, *a → b == 99, c == [1, 2, 3, 4]
Can someone please throughly explained why in Ruby the asterisk makes the code return what it returns? I understand that the if an lvalue has an asterisk, it assigns rvalues to that lvalues. However, why does '*a' make 'c' return only the '1' value in the array and why does '*a' and '*c' cancel each other out?
In both cases, 99, *a on the right-hand side expands into the array [99, 1, 2, 3, 4]
In
b, c = 99, *a
b and c become the first two values of the array, with the rest of the array discarded.
In
b, *c = 99, *a
b becomes the first value from the array and c is assigned the rest (because of the splat on the left-hand side).
The 99, *a on the right-hand side is an example of where the square brackets around an array are optional in an assignment.
A simpler example:
a = 1, 2, 3 → a == [1, 2, 3]
Or a more explicit version of your example:
example = [99, *a] → example == [99, 1, 2, 3, 4]

How to group values in double dimension table?

I try to group values in a double dimension table in an other table, but without duplicates. All attempts i made create a table with duplicates.
Here's an example:
This is my table:
tab1 = {
{id = "id1", dmg = 0, qty = 1},
{id = "id2", dmg = 0, qty = 1},
{id = "id3", dmg = 0, qty = 1},
{id = "id1", dmg = 0, qty = 1},
}
and i would like an other table like that:
tab2 = {
{id = "id1", dmg = 0, qty = 2},
{id = "id2", dmg = 0, qty = 1},
{id = "id3", dmg = 0, qty = 1},
}
So i want my qty values to be summed and tables to be groupe like in the example. Does Someone have an idea for this problem ? Is there a function to do this ?
Thanks for your answers. Sorry if my english is bad, it's not my native language.
There is no builtin function for this. You have to write your own. Here is my take.
tab1 = {
{id = "id1", dmg = 0, qty = 1},
{id = "id2", dmg = 0, qty = 1},
{id = "id3", dmg = 0, qty = 1},
{id = "id1", dmg = 0, qty = 1},
}
local a={}
for k,v in ipairs(tab1) do
local id=v.id
if a[v.id]==nil then
a[v.id] = { id=v.id, dmg=v.dmg, qty=v.qty }
else
a[v.id].qty=a[v.id].qty+v.qty
end
end
local tab2={}
local n=0
for k,v in pairs(a) do
n=n+1
tab2[n]=v
end
table.sort(tab2, function (a,b) return a.id < b.id end)
for k,v in ipairs(tab2) do
print(k,v.id,v.dmg,v.qty)
end
Here's one possibility. (I assume your unique key is just id. If not, adjust accordingly. Also, I only add qty, you may need to also add dmg.)
tab1 = {
{id = "id1", dmg = 0, qty = 1},
{id = "id2", dmg = 0, qty = 1},
{id = "id3", dmg = 0, qty = 1},
{id = "id1", dmg = 0, qty = 1},
}
function tablecopy(t)
local ans = {}
for k,v in pairs(t) do
ans[k] = v
end
return ans
end
function nodups(t)
local temp = {} --workspace
for _,t in ipairs(t) do
if temp[t.id] == nil then
temp[t.id] = tablecopy(t)
else
temp[t.id].qty = temp[t.id].qty + t.qty
--temp[t.id].dmg = temp[t.id].dmg + t.dmg
end
end
-- and, if you need to convert to array
local t = {}
for _,v in pairs(temp) do
t[#t+1] = v
end
return t
end
tab2 = nodups(tab1)

How to read nested table

I am new to lua and want to learn about nested table
This is what I have been trying:
t = {};
t[1] = 22, {1, 22, 3};
t[2] = 44, {4, 5, 6};
t[3] = 66, {4, 5, 6};
for k, v in ipairs(t) do
print(k,v)
end
This does not work for me.
This is the result I want:
Example:
1 2 5
2 5 66
3 6 33
4 2 1
5 4 12
6 4 3
7 2 44
8 3 1
9 2 44
10 3 99
How do I read a nested table like this and
how do I table.insert in the right column?
I believe the misunderstanding lies in this code:
t[1] = 22, {1, 22, 3}
The 2nd value, the one after the comma, {1, 22, 3} is being assigned to nothing, it is dropped in the ether.
Perhaps what you meant was a table of tables:
t[1] = {22, {1, 22, 3}}
This would create an entry at [1] in t that is itself..a table
t[1][1] == 22
t[1][2] == {1, 22, 3}
Then to iterate this setup you could use:
local t = {}
t[1] = {22, {1, 22, 3}}
t[2] = {44, {4, 5, 6}}
t[3] = {66, {4, 5, 6}}
for _,entry in ipairs(t) do
local key = entry[1]
io.write(key .. ' ')
for _,value in ipairs(entry[2]) do
io.write(value .. ' ')
end
io.write('\n')
end
But to be honest, I'm not sure what you're asking as your "expected output" is significantly different than your sample data set.
Replying to Qualmos' comment in payo's answer:
local t = {}
t[1] = {22,3}
t[2] = {44,6}
t[3] = {66,63}
for _,v in pairs(table) do
print(_,v[1],v[2])
end
Would print something like this:
1,22,3
2,44,6
3,66,63
Btw, you can make the table look like this:
local t = {
{22,3};
{44,6};
{66,63};
}

Resources