[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [sc-users] array processing



Thanks Batuhan,

But I was looking for something more modular.  I ended up doing this (pasted below).  If anyone wants to look at it and suggest something cleaner/more efficient, any help is appreciated.

I plan on making a method for SequenceableCollection out of it.  Would it be the right place for it ?

And... is there already a method somewhere that does something similar ?


Martin Marier

(
d=[
"/pen/0", [
["/0", "/1", "/2", "/3", "/4"],
"/button",
["/1", "/2", "/3"],
"/proximity"],
"/key",
["/1", "/2", "/3", "/4"],
"/strip/1",
["/0", "/1"],
"/eraser/0",
[["/0", "/1", "/2", "/3", "/4"],
"/button",
["/1","/2","/3"],
"/proximity"];
];
f={|array, base|
var result;
if(base.isString){
base = List[base]
}{
base = base ? List[];
};
base.add("");
array.do{|i,j|
if(i.isArray and: i.isString.not){ // item is an Array
result = result ++ f.value(i, base);
base.pop;
}{ //item is a String
if(array[j+1].isArray and: array[j+1].isString.not){
//next item is an Array.
base.add(i);
}{ //next item is not an Array.
result = result.add(base.inject("",_++_)++i);
if(array[j+1].isNil){
base.pop;
}
};
};
};
result;
}
)
f.value(d,"/wacom/1")

On 28-Jul-09, at 3:44 PM, Batuhan Bozkurt wrote:

Hi Martin, since there is no fixed algorithm in your concatenation scheme, this is not a single looping structure but the .collect and the ++ operator for concatenating arrays and symbols (and strings) are your friends here, something like this:

a = [ '/pen/0', [ [ '/0', '/1', '/2', '/3', '/4' ], '/button', [ '/1', '/2', '/3' ], '/proximity' ], '/key', [ '/1', '/2', '/3', '/4' ] ]

a[1][0].collect({|item| a[0]++item; }) ++ a[1][2].collect({|item| a[0]++a[1][1]++item }) ++ [a[0] ++ a[1][3]] ++ a[3].collect({|item| a[2]++item })
 
Batuhan Bozkurt




On Jul 28, 2009, at 8:26 AM, Martin Marier wrote:

Hello list,

I'm stuck on what seemed to be a simple problem at first.  There must be a simple solution...

I want an array structured like this :

[ /pen/0, [ [ /0, /1, /2, /3, /4 ], /button, [ /1, /2, /3 ], /proximity ], /key, [ /1, /2, /3, /4 ] ]

to end up like this :

[ /pen/0/0, /pen/0/1, /pen/0/2, /pen/0/3, /pen/0/4, /pen/0/button/1, /pen/0/button/2, /pen/0/button/3, /pen/0/proximity, /key/1, /key/2, /key/3, /key/4 ]




It is clearer presented this way (I think):

[ /pen/0,
[
[ /0, /1, /2, /3, /4 ],
/button,
[ /1, /2, /3 ],
/proximity
],
/key,
[ /1, /2, /3, /4 ]
]

Any suggestions ?

Martin Marier

_______________________________________________
sc-users mailing list

info (subscription, etc.): http://www.beast.bham.ac.uk/research/sc_mailing_lists.shtml
archive: https://listarc.bham.ac.uk/marchives/sc-users/
search: https://listarc.bham.ac.uk/lists/sc-users/search/