Create structures in loop and assign to another structure in IDL - structure

In IDL I would like to create a structure in a loop with different data and then to assign them all to the main structure that will zip everything.
I tried to used an array of structures, but I am stopped because I am not able to assign to the main structure:
alarm_list = { rg : 0, rf : 4}
alarm = { $
Alarm_Id : 0 , $
Range : 1, $
Bin : 0 $
}
arr = REPLICATE(alarm, 4)
FOR ia = 0, 3 DO BEGIN
alarm.alarm_id = ia
alarm.bin = bin
arr[ia] = alarm
bin += 1
ENDFOR
I would like to assign all 4 alarms with different names (i.e. alarm1 = , alarm2 = ...) to the main "alarm_list".
Thank you for your answers.

You could try something along these lines, assigning to the array directly instead of using the alarm variable as an intermediate:
alarm_list= REPLICATE({alarm, Alarm_Id:0, Range:1, Bin:0},4)
FOR ia = 0, 3 DO BEGIN
alarm_list[i].alarm_id = ia
alarm_list[i].bin = bin
bin += 1
ENDFOR
Hope this helps guide.

Related

using paste0 in file name in exams2moodle

I am trying to create a loop to automatize exams generation using the examspackage....
I have created a series of exercices like this
gr1 <- c("ae1_IntroEst_1.Rmd","ae1_IntroEst_2.Rmd","ae1_IntroEst_3.Rmd","ae1_IntroEst_4.Rmd")
gr2 <- c("ae1_IntroProcEst_1.Rmd","ae1_IntroProcEst_2.Rmd","ae1_IntroProcEst_3.Rmd","ae1_IntroProcEst_4.Rmd")
...etc...
Now, I am creating a loop to export all the exercices to moodle xml:
for (i in 1:2){
grupo <- paste0("gr",i)
exams2moodle(grupo, name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
}
But I am getting this error:
Error in xexams(file, n = n, nsamp = nsamp, driver = list(sweave = list(quiet = quiet, : The following files cannot be found: gr11.
If I replace "grupo" by "gr1" then it works... (but I am generating 20 exercices). I can't figure it out...
Any ideas?
Thanks!
Because grupo is a string: "gr1". The exams2moodle's first parameter is a string (in your case) and not the list of files (as you want).
If you want use a variable which name is in a string variable, you should use get (get: Return the Value of a Named Object)
Check the sample code:
> x <- 'foo'
> foo <- 'bar'
> x
[1] "foo"
> get(x)
[1] "bar"
>
In your case:
for (i in 1:2){
grupo <- paste0("gr",i)
exams2moodle(get(grupo), name = paste0("mt1_",i, "_M"), dir = "nops_moodle", encoding = "UTF-8", schoice = list(answernumbering = "none", eval = ee))
}

Lua get index name of table as table

Is there any way to get every index value of a table?
Example:
local mytbl = {
["Hello"] = 123,
["world"] = 321
}
I want to get this:
{"Hello", "world"}
local t = {}
for k, v in pairs(mytbl) do
table.insert(t, k) -- or t[#t + 1] = k
end
Note that the order of how pairs iterates a table is not specified. If you want to make sure the elements in the result are in a certain order, use:
table.sort(t)

Pentaho to convert tree structure data

I have a stream of data from a CSV. It is a flat structured database.
E.g.:
a,b,c,d
a,b,c,e
a,b,f
This essentially transforms into:
Node id,Nodename,parent id,level
100, a , 0 , 1
200, b , 100 , 2
300, c , 200 , 3
400, d , 300 , 4
500, e , 300 , 4
600, f , 200 , 3
Can this be done using Pentaho? I have gone through the transformation steps. But nothing strikes me as usable for this purpose. Please let me know if there is any step that I may have missed.
Your CSV file contains graph or tree definition. The output format is rich (node_id needs to be generated, parent_id needs to be resolved, level needs to be set). There are few issues you will face when processing this kind of CSV file in Pentaho Data Integration:
Data loading & processing:
Rows do not have same length (sometimes 4 nodes, sometimes 3 node).
Load whole rows. And then split rows to nodes and process one node per record stream item.
You can calculate output values in the same step as where the nodes are split.
Solution Steps:
CSV file input: Load data from CSV. Settings: No header row; Delimiter = ';'; One output column named rowData
Modified Java Script Value: Split rowData to nodes and calculate output values: nodeId, nodeName, parentId, nodeLevel [See the code below]
Sort rows: Sort rows by nodeName. [a,b,c,d,a,b,c,e,a,b,f >> a,a,a,b,b,c,c,d,e,f]
Unique rows: Delete duplicate rows by nodeName. [a,a,a,b,b,c,c,d,e,f >> a,b,c,d,e,f]
Text file output: Write out results.
Modified Java Script Value Code:
function writeRow(nodeId, nodeName, parentId, nodeLevel){
newRow = createRowCopy(getOutputRowMeta().size());
var rowIndex = getInputRowMeta().size();
newRow[rowIndex++] = nodeId;
newRow[rowIndex++] = nodeName;
newRow[rowIndex++] = parentId;
newRow[rowIndex++] = nodeLevel;
putRow(newRow);
}
var nodeIdsMap = {
a: "100",
b: "200",
c: "300",
d: "400",
e: "500",
f: "600",
g: "700",
h: "800",
}
// rowData from record stream (CSV input step)
var nodes = rowData.split(",");
for (i = 0; i < nodes.length; i++){
var nodeId = nodeIdsMap[nodes[i]];
var parentNodeId = (i == 0) ? "0" : nodeIdsMap[nodes[i-1]];
var level = i + 1;
writeRow(nodeId, nodes[i], parentNodeId, level);
}
trans_Status = SKIP_TRANSFORMATION;
Modified Java Script Value Field Settings:
Fieldname; Type; Replace value'Fieldname' or 'Rename to'
nodeId; String; N
nodeName; String; N
parent_id; String; N
nodeLevel; String; N

Accessing an int[] inside an ArrayList

I'm trying to make a program that reads points (e.g. (2,4), (0,0) ) from a file and tried using ArrayList since I don't know how many many points there will be. However only the last read point seems to get stored. I think only the pointer of nInput[] gets stored and not the actual value hence any subsequent sc.next() only seem to change the existing nInput[] in the ArrayList.
Ex input file:
-1 2
0 1
0 0 <<< I would get this as the only output
OR
-1 2
0 4
9 9 << This would be the only output
int nInput = new int[2];
ArrayList<int[]> aPoints = new ArrayList<int[]>();
while(sc.hasNext()){
nInput[0] = Integer.parseInt(sc.next());
nInput[1] = Integer.parseInt(sc.next());
aPoints.add(nInput);
}
for(int i<0; i<aPoints.size(); i++)
System.out.println(aPoints.get(i)[0]+" "+ aPoints.get(i)[1]);
How do I store int[] into an ArrayList?
Edit: First I gave pseudocode but I think you need Java code:
(I assume sc as a Scanner object)
ArrayList<int[]> list = new ArrayList<int[]>();
while(sc.hasNext) {
int[] array = new int[2];
array[0] = sc.nextInt();
array[1] = sc.nextInt();
list.add(array);
}

Can't inherit table attribute from parent

Sprite = {x = 0, y = 0, pos = {z=0}}
function Sprite:new()
o = {}
setmetatable(o,self)
self.__index = self
return o
end
s1 = Sprite:new()
s2 = Sprite:new()
s1.x = 10
s1.pos.z = 5
print("s1.x", s1.x, "s2.x", s2.x, "s1.z",s1.pos.z, "s2.z", s2.pos.z )
s2.x = 20
s2.pos.z = 50
print("s1.x", s1.x, "s2.x", s2.x, "s1.z",s1.pos.z, "s2.z", s2.pos.z )
In the above code snippet, I define a class Sprite which has x(int),y(int),pos(table) 3 attributes, but when I init two object s1,s2. I found that they shared the pos attribute.
If you run the code, it will print:
s1.x 10 s2.x 0 s1.z 5 s2.z 5
s1.x 10 s2.x 20 s1.z 50 s2.z 50
s1 and s2 has their own x,y attribute, but share pos attribute, if s1.pos.z is changed, so as the s2.pos.z.
How can I fix this?
In Sprite:new, the variable self always has Sprite as its value. So, self.pos refers to Sprite.pos. Try changing to o.pos={}. Also, consider making o a local.
Sprite represents the class, hence you can think of Sprite table as the "class wide" entries: they will be shared by all "instances". Instance specific entries should be in the o table:
Sprite = {classX = 0, classY = 0} -- class; vars shared by all instances
function Sprite:new()
o = {pos = {z=0}}
setmetatable(o,self)
self.__index = self
return o
end
Note that "shared" really does mean shared at the reference level, so all instances will see the same values and any changes made by one instance will be seen by all others. OTOH data you put in o table is per instance. Putting this in Sprite.new() ensures that all instances have the same fields, but their own data; changes by one instance will not affect any other instance.
That said, your Sprite:new() does not define self.__newindex. So Sprite.classX = 5 will be seen by all instances, as expected, but s1.classX = 6 will only be seen by s1: it will create a new field, thus hiding that of Sprite. From then on, changes to Sprite.classX will no longer be seen by s1 (but will be by all other instances that have not overridden Sprite.classX). To get around that, you could do this:
function Sprite:new()
o = {pos = {z=0}}
setmetatable(o,self)
self.__index = self
self.__newindex = self
return o
end
In Lua console you would see this if you played around with this:
> s1=Sprite:new()
> s2=Sprite:new()
> print(s1.classX, s2.classX)
0 0
> Sprite.classX=1
> print(s1.classX, s2.classX)
1 1
> s1.classX=3
> print(s1.classX, s2.classX)
3 3
Without that change, that last output will show "3 1" and changes to Sprite.classX would not be visible in s1.
Lua shares tables(keeps them as reference), and copy variables. Use metatables for methods, and keep fields copying them in your object table.
Sprite = {
instanceData = { x = 0, y = 0, pos = {z = 0} },
method = function(self) print("do smth with "..self) end
}
function Sprite:new()
local o = deepCopy(self.instanceData)
setmetatable(o,self)
self.__index = self
return o
end
deep copy implementation can be found wiki/CopyTable