array_vars.ion 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. echo '# ANCHOR: array_assignments'
  2. let array = [1 2 3]
  3. let array[0] = 4
  4. echo @array
  5. let value = 1
  6. let array[$value] = 5
  7. echo @array
  8. let array[2]:int = 6
  9. echo @array
  10. echo '# ANCHOR_END: array_assignments'
  11. echo '# ANCHOR: array_ops'
  12. let array = [ 4 4 5 5 5 6 6 6 6 ]
  13. echo @array
  14. let array ++= [ 1 2 3 ]
  15. echo @array
  16. let array ::= [ 1 2 3 ]
  17. echo @array
  18. let array \\= [ 4 5 6 ]
  19. echo @array
  20. let array = [ 1 2 2 3 3 3 ]
  21. echo @array
  22. let array ++= 4
  23. echo @array
  24. let array ::= 0
  25. echo @array
  26. let array \\= 3
  27. echo @array
  28. echo '# ANCHOR_END: array_ops'
  29. echo '# ANCHOR: create_array'
  30. let array = [ one two 'three four' ]
  31. echo '# ANCHOR_END: create_array'
  32. echo '# ANCHOR: index_array'
  33. let array = [ 1 2 3 4 5 ]
  34. echo @array[0]
  35. echo @array[2..=4]
  36. echo '# ANCHOR_END: index_array'
  37. echo '# ANCHOR: array_copy'
  38. let array = [ 1 2 3 ]
  39. let array_copy = [ @array ]
  40. echo @array_copy
  41. echo '# ANCHOR_END: array_copy'
  42. echo '# ANCHOR: array_join'
  43. let array = [ hello world ]
  44. let other_array = [ this is the ion ]
  45. let array = [ @array @other_array shell ]
  46. let as_string = @array
  47. echo @array
  48. echo $as_string
  49. echo '# ANCHOR_END: array_join'
  50. echo '# ANCHOR: array_concat_var_strip'
  51. let array = [2 3]
  52. let array ++= [4 5] # append
  53. let array ::= [0 1] # append before beginning [0 1]
  54. let array \\= [2 3] # remove variables 2 and 3
  55. echo @array
  56. let array ++= 6 # same with single variables
  57. let array ::= -1
  58. let array \\= 0
  59. echo @array
  60. echo '# ANCHOR_END: array_concat_var_strip'
  61. echo '# ANCHOR: practical_array'
  62. mkdir -p _tmp _tmp/t1 _tmp/t2
  63. cd _tmp
  64. let args = [-a --file-type]
  65. ls @args # use args as arguments for command ls
  66. let res = [ @(ls) ] # get result of ls as array res
  67. echo @res # output the array res
  68. cd ..
  69. rm -fr _tmp
  70. echo '# ANCHOR_END: practical_array'