Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extend an array in YAML?

Tags:

yaml

Suppose I have:

base_array:   -1   -2 

how could I do something like:

my_array: << base_array   -3 

so that my_array was [1,2,3]

Update: I should specify that I want the extending to occur inside the YAML itself.

like image 287
bgcode Avatar asked Oct 21 '13 18:10

bgcode


1 Answers

Since the already commented issue#35 exists, merge-keys << doesn't help you. It only merges/inserts referenced keys into a map (see YAML docs merge). Instead you should work with sequences and use anchor & and alias *.

So your example should look like this:

base_list: &base     - 1     - 2  extended: &ext     - 3  extended_list:     [*base, *ext] 

Will give result in output like this (JSON):

{   "base_list": [     1,      2   ],    "extended": [     3   ],    "extended_list": [     [       1,        2     ],      [       3     ]   ] }  

Although not exactly what you expected, but maybe your parsing/loading environment can flatten the nested array/list to a simple array/list.

You can always test YAML online, for example use:

  • http://ben-kiki.org/ypaste
  • Online YAML Parser
like image 184
hc_dev Avatar answered Oct 05 '22 22:10

hc_dev