You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I find when I am using [[foreach]] question blocks, I often am using it to shortcut writing out a list of variables. As such, I want to put commas and a space between each run of the foreach loop except the last one, as I don't want an extra comma and space at the end. My solution so far is to either not use the forloop (if there aren't very many elements), or drop out of it one element early so that I can add in the special case manually.
Would it be possible to either add a "seperator" argument that inserts whatever you input between each run of the forloop?
The most recent example of this phenomena is "[[foreach j="makelist(i,i,1,length(complexNumbers))"]] z_{@j@} = {@ComplexNumbers[j]@}, \quad [[/ foreach]]", which, for my case, would output
z_1 = x_1 + i y_1, z_2 = x_2 + i y_2, z_3 = x_3 + y_3, (This is where the quad ends)
I hope others would find this useful too, or if anyone has a workaround, it would be much appreciated,
Jake
The text was updated successfully, but these errors were encountered:
Bennett-Woolf
changed the title
Add Seperators For Foreach Question Block
Proposal: Add Seperators For Foreach Question Block
Aug 31, 2023
#917 might have an interesting use of [[define]] in one of the suggestions that might work for this as well. But yes someone could possibly add such an extra separator argument to the block. It would only reserve one identifier for something that could not be used as a name of an iteration variable. Not promising to add such a thing though, maybe some newer coder might want to wrap their head around this and add such logic to this. We might at the same time consider if we need to bring other common loop concepts, like loop local flags for first and last executions etc. into the block.
Basically, I would write your example like this, in a world where we would not have such a separator option:
Basically, [[define/]] allows one to change the values of variables and define new ones within the text, including in the conditional and loopy bits...
I've been thinking about your response (and had the situation happen again) and a simpler solution to my problem is do exactly what you're saying but without the define blocks and just manually unroll the first loop, giving us
which avoids having an extra conditional. The idea of unrolling the first rather than the last item in the loop is exactly what I needed so thank you so much for that.
Unfortunately my PHP knowledge is completely non-existent, otherwise I would love to add foreach constructs myself.
Hi all,
I find when I am using [[foreach]] question blocks, I often am using it to shortcut writing out a list of variables. As such, I want to put commas and a space between each run of the foreach loop except the last one, as I don't want an extra comma and space at the end. My solution so far is to either not use the forloop (if there aren't very many elements), or drop out of it one element early so that I can add in the special case manually.
Would it be possible to either add a "seperator" argument that inserts whatever you input between each run of the forloop?
The most recent example of this phenomena is "[[foreach j="makelist(i,i,1,length(complexNumbers))"]] z_{@j@} = {@ComplexNumbers[j]@}, \quad [[/ foreach]]", which, for my case, would output
z_1 = x_1 + i y_1, z_2 = x_2 + i y_2, z_3 = x_3 + y_3, (This is where the quad ends)
I hope others would find this useful too, or if anyone has a workaround, it would be much appreciated,
Jake
The text was updated successfully, but these errors were encountered: