diff --git a/docs/syntax/for.md b/docs/syntax/for.md index 90a50b75..b26563e3 100644 --- a/docs/syntax/for.md +++ b/docs/syntax/for.md @@ -56,12 +56,16 @@ echo(y) # 9 ## In form The "in" form of the `for` loops allows you to iterate over -an array: +an array or an hash: ``` bash for x in [1, 2, 3] { # x is 1, 2, 3 } + +for x in {"a": 1, "b": 2, "c": 3} { + # x is 1, 2, 3 +} ``` Both key and values are available in the loop: @@ -71,6 +75,11 @@ for k, v in [1, 2, 3] { # k is 0, 1, 2 # v is 1, 2, 3 } + +for k, v in {"a": 1, "b": 2, "c": 3} { + # k is a, b, c + # v is 1, 2, 3 +} ``` In terms of scoping, the "in" form follows the same rules @@ -79,13 +88,13 @@ as the standard one, meaning that: ``` bash k = "hello world" -for v in [1, 2, 3] { +for k, v in [1, 2, 3] { # k is 0, 1, 2 # v is 1, 2, 3 } -echo(v) # "hello world" -echo(k) # v is not defined +echo(k) # "hello world" +echo(v) # v is not defined ``` ## Next diff --git a/docs/types/hash.md b/docs/types/hash.md index a9a081ea..eb2a74f0 100644 --- a/docs/types/hash.md +++ b/docs/types/hash.md @@ -12,7 +12,10 @@ Note that the `hash.key` hash property form is the preferred one, as it's more c Accessing a key that does not exist returns null. -An individual hash element may be assigned to via its `hash["key"]` index or its property `hash.key`. This includes compound operators such as `+=`. Note that a new key may be created as well using `hash["newkey"]` or `hash.newkey`. +An individual hash element may be assigned to via its `hash["key"]` +index or its property `hash.key`. This includes compound operators +such as `+=`. Note that a new key may be created as well using `hash["newkey"]` or `hash.newkey`: + ```bash h = {"a": 1, "b": 2, "c": 3} h # {a: 1, b: 2, c: 3} @@ -37,7 +40,9 @@ h.y = 20 h # {a: 88, b: 2, c: 3, x: 10, y: 20} ``` -It is also possible to extend a hash using the `+=` operator with another hash. Note that any existing keys on the left side will be replaced with the same key from the right side. +It is also possible to extend a hash using the `+=` operator +with another hash. Note that any existing keys on the left side +will be replaced with the same key from the right side: ```bash h = {"a": 1, "b": 2, "c": 3} @@ -48,7 +53,9 @@ h += {"c": 33, "d": 4, "e": 5} h # {a: 1, b: 2, c: 33, d: 4, e: 5} ``` -In a similar way, we can make a **shallow** copy of a hash using the `+` operator with an empty hash. Be careful, the empty hash must be on the left side of the `+` operator. +In a similar way, we can make a **shallow** copy of a hash using +the `+` operator with an empty hash. Be careful, the empty hash +must be on the left side of the `+` operator: ```bash a = {"a": 1, "b": 2, "c": 3} @@ -65,7 +72,10 @@ b # {a: 99, b: 2, c: 3} a # {a: 1, b: 2, c: 3} ``` -If the left side is a `hash["key"]` or `hash.key` and the right side is a hash, then the resulting hash will have a new nested hash at `hash.newkey`. This includes `hash["newkey"]` or `hash.newKey` as well. +If the left side is a `hash["key"]` or `hash.key` and the +right side is a hash, then the resulting hash will have a +new nested hash at `hash.newkey`. This includes `hash["newkey"]` +or `hash.newKey` as well: ```bash h = {"a": 1, "b": 2, "c": 3}