Transpose a 2D record

17 views
Skip to first unread message

Ville Rantanen

unread,
Aug 9, 2013, 8:33:41 AM8/9/13
to
Ever had to iterate and collect data from a component that produces several outputs?   
You might have this kind of syntax:

orig={}
files
={}
for x:std.range(1,200) {
    orig
[x]=BashEvaluate(script="echo foo > @optOut1@; echo "+x+" >> @optOut1@;")
    files
[x]=orig[x].optOut1
}
collectedFiles
=Array2Folder(files,fileMode="@key@.txt")

Now, this may not look like an improvement, but often you don't want to collect several records in a for-loop:
First we need to create a function for the transpose:
function transpose(record a,record b)->() {
   
for i,x,v:std.enumerate(a) { for y,u:v {
       
if i==1 { b[y]={} } b[y][x]=u }}
   
return {}
}

now we can formulate the for-loop with a single record:

orig={}
for x:std.range(1,200) {
    orig
[x]=BashEvaluate(script="echo foo > @optOut1@; echo "+x+" >> @optOut1@;")
}  
files
={}
transpose
(a=orig,b=files)
collectedFiles
=Array2Folder(files.optOut1,fileMode="@key@.txt")

In short, the orig record contains two levels of indexes: the first is running from 1 - 200, and the second are the output ports of the BashEvaluate component (optOut1-3, folder1-3, arrays ... etc)
After the transpose, the files  record has the same two levels, but in reversed order.

Reply all
Reply to author
Forward
0 new messages