How to access key values in nested arrays within a function?

How can I print the value for test1 in the first array?

~test = {|i| [ [test1: 0, test2: 1], [test1: 2, test2: 3] ] };
~test.(i).collect({ |args|
        //Below prints "0" and "2". I am just trying to access the value in the first array
	args[1].postln;
});

Thank you

I don’t understand why you make a function and a collect so here is a more generic example first:

~twoDarray = [[1,2],[11,22]]

~twoDarray[0][0] // 1
~twoDarray[1][0] // 11

I don’t understand your question of accessing a key value. Keys are not in Arrays but a name of a ‘slot’ in a Dictionary… Arrays are ordered, not keyed, hence using numbers to reach a value at a given slot.

A bit more context will help me help you better but I hope this helps already a bit?

Apologies for the lack of context and thank you for the clarification on Keys / Arrays.

Below is a more detailed overview of what I am trying to do:

Here is my function:

~synth_arg_funcs = {|i| [
        //Array 1
	[
		patch: ~b1,
		amp: [1, 1, 1, 1].wrapAt(i),
		buffer: [4, 4, 4, 8, 12].wrapAt(i),
		rate: [0.8].wrapAt(i),
		atk: [0.01].wrapAt(i),
		rel: [0.01].wrapAt(i)
	],
        /Array 2
	[
		patch: ~b1,
		amp: [1, 1, 1, 1].wrapAt(i),
		buffer: [1, 2, 2, 1, 9].wrapAt(i),
		rate: [0.9].wrapAt(i),
		atk: [0.01].wrapAt(i),
		rel: [0.01].wrapAt(i)
	],
] };

This is my main loop. I am using the args from above to populate the args for a Synth. It is working very similarly to a Pseq but I am trying to manipulate the buffer values in ‘Array 1’ while not manipulating the values in ‘Array 2’.

For example, for every iteration add +1 to the buffer values in Array 1.

loop {
	16.do { |synth, i|
		var synthArgs = ~synth_arg_funcs.(i).collect({|args|
			Synth(\mainbuf, args: args, target: ~sources);
		});
		s.makeBundle(s.latency, {
			synthArgs.do { |synth| synth.set(\gate, 0) };
		});
		tatum.wait;
	};
};
a = [testA:1, testB:2]
a.asEvent[\testA]

You have an array, but want to access it like an event, there are many ways to convert between, but the default works here.

Why not:

~synth_arg_funcs = {|i| [
        //Array 1
	[
		patch: ~b1,
		amp: [1, 1, 1, 1].wrapAt(i),
		buffer: [4, 4, 4, 8, 12].wrapAt(i) + i, // here, just add i
		rate: [0.8].wrapAt(i),
		atk: [0.01].wrapAt(i),
		rel: [0.01].wrapAt(i)
	],
        /Array 2
	...
] };

Hey @jordan,

Thanks for your help on all of this.

I actually tried this approach and it is close to what I’m looking for, but I would like it to add +1 upon each iteration. When I add i here, it is adding + i once and then the pattern repeats the same.

buffer: [4, 4, 4, 8, 12].wrapAt(i) + i, // here, just add i

My goal is to create something that is ‘evolving’ / cycling through manipulations.

That is exactly what that does, it just has the evolution number parameterised as an input. If you want to embed this in another loop, just add another input to the function, j for example.

I’d suggest sticking with that approach because managing the internal state of some other object can become complex rather quickly - although for simple cases it is often easier.

Nevertheless, you would use a Routine for this type of thing. This is how you can do it:

r = Routine({
	var i=0;
	loop {
		[
			patch: ~b1,
			amp: (1!4).wrapAt(i),
			buffer: [4, 4, 4, 8, 12].wrapAt(i) + i,
			rate: [0.8].wrapAt(i),
			atk: [0.01].wrapAt(i),
			rel: [0.01].wrapAt(i)
		].yield;
		i = i + 1;
	}
});

r.next;
r.next;
r.next;

Every time you call .next on the routine, the array is returned (yielded) and i is incremented. Now you can very easily declare new variables and use those to store the running state and manipulate those as you please.

Ah, I see it now!

Thanks again, @jordan I appreciate your help on these questions.