how to update a variable defined out side of range from the range

Viewed 31
. as $index | 1 as $runner | label $out | range($index; 20) 
| if (. == 10) then break $out else "dx" + ($runner|tostring) | ($runner+1) as $runner | . end

for input 1

I would like to update $runner variable value while looping using range.

demo

Thanks in advance

1 Answers

You cannot "update" a variable in the same context, you can only shadow it by setting its value for a new context, based on the previous.

That said, range already iterates in your sense, no need to +1 anywhere. Store the value of the iteration itself to the $runner variable, and you can use it as envisioned. To filter out certain values, use select, no need for label and break.

. as $index | range($index; 20) as $runner
| select($runner < 10) | "dx\($runner)"
"dx1"
"dx2"
"dx3"
"dx4"
"dx5"
"dx6"
"dx7"
"dx8"
"dx9"

Demo


If you don't need the variables elsewhere, you can even iterate without variables. A condensed version ofthe above:

range(.; 20) | "dx\(select(. < 10))"

Demo

Related