Loops/Foreach: Difference between revisions
Content deleted Content added
→{{header|jq}}: range and keys |
→{{header|jq}}: iterables and strings |
||
Line 954: | Line 954: | ||
The Julia <code>for</code> statement is always a "foreach", and the built-in <code>start:end</code> or <code>start:step:end</code> "range" syntax can be used for iteration over arithmetic sequences. Many Julia objects support iteration: arrays and tuples iterate over each item, strings iterate over each character, dictionaries iterate over (key,value) pairs, numeric scalars provide a length-1 iteration over their value, and so on. |
The Julia <code>for</code> statement is always a "foreach", and the built-in <code>start:end</code> or <code>start:step:end</code> "range" syntax can be used for iteration over arithmetic sequences. Many Julia objects support iteration: arrays and tuples iterate over each item, strings iterate over each character, dictionaries iterate over (key,value) pairs, numeric scalars provide a length-1 iteration over their value, and so on. |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
'''Iterables''': |
|||
In this section, the array defined by "example" is used as an example: |
In this section, the array defined by "example" is used as an example: |
||
<lang jq>def example: [1,2 |
<lang jq>def example: [1,2];</lang> |
||
jq has two types of iterables -- JSON arrays and JSON objects. In both cases, the ".[]" filter may be used to iterate through the values, it being understand that for objects, the "values" are the values associated with the keys: |
|||
The idiomatic way in jq to accomplish the task, that is, to generate a stream of the items in an array, is to pipe the array through the ".[]" filter, or to append "[]" to the array, e.g. |
|||
<lang jq>example[] |
<lang jq>example | .[] |
||
# or equivalently: example[]</lang> |
|||
The .[] filter can also be used to generate a stream of the values of a JSON object, |
|||
<lang jq>{"a":1, "b":2} | .[] |
|||
e.g. |
|||
# or equivalently: {"a":1, "b":2}[]</lang> |
|||
In both cases, the output is the stream consisting of the values 1 followed by 2. |
|||
produces in turn 1 and 2. |
|||
Sometimes it is necessary to use an alternative to ".[]. For example, one might want to generate an index along with the array. In such cases, the "range(m;n)" generator, which performs a similar role to C's "for(i=m; i<n; i++)", can be used. Here is how range/2 would be used to perform the task: |
Sometimes it is necessary to use an alternative to ".[]". For example, one might want to generate an index along with the array elements. In such cases, the "range(m;n)" generator, which performs a similar role to C's "for(i=m; i<n; i++)", can be used for array. Here is how range/2 would be used to perform the task for an array: |
||
<lang jq>range(0; example|length) | example[.]</lang> |
<lang jq>range(0; example|length) | example[.]</lang> |
||
⚫ | |||
<lang jq> |
|||
⚫ | |||
</lang> |
|||
⚫ | |||
⚫ | |||
⚫ | |||
'''Strings''': |
|||
⚫ | |||
To convert the constituent characters (or technically, codepoints) of a string into a stream of values, there are two techniques illustrated by these examples: |
|||
⚫ | |||
<lang jq>"abc" | . as $s | range(0;length) | $s[.:.+1] |
|||
"abc" | explode | map( [.]|implode) | .[]</lang> |
|||
⚫ | |||
In both cases, the result is the stream of values: "a", "b", "c". |
|||
=={{header|Lasso}}== |
=={{header|Lasso}}== |