unwanted flattening of arrays

14 views
Skip to first unread message

Kjetil Torgrim Homme

unread,
Dec 2, 2009, 7:43:44 PM12/2/09
to puppet...@googlegroups.com
if you do

$var = [[1,2,3], [4,5,6]]

$var will be set to [1,2,3,4,5,6]. you can observe this with a template
like

<% var.each do |el| -%>
array element: <%= el %>
<% end -%>

on the other hand,

$a = [1,2,3]
$b = [4,5,6]
$var = [$a, $b]

will preserve the arrays within the array, which means you can do a
nested loop in your template:

<% var.each do |arr| -%>
array {
<% arr.each do |el| -%>
<%= el %>
<% end -%>
}
<% end -%>

defining lots of "dummy" variables will just make the config less easy
to read, so the current best workaround is to use a string and split it
on some character which hopefully won't be used in any of the values.
ugly.

question: does anyone really depend on this flattening of arrays which
are specified as immediate values, or could this behaviour change?

this issue is related to http://projects.reductivelabs.com/issues/2226


I tried to work around the problem by defining a function which would do
this for me, e.g.,

$var = concat([1,2,3], [4,5,6])

unfortunately, even though the function works for simple values, with
these immediate array values the parser barfs:

Syntax error at '['; expected ']'


I'm using 0.24.8, please let me know if this behaviour has changed in
0.25.x.
--
Kjetil T. Homme
Redpill Linpro AS - Changing the game

Reply all
Reply to author
Forward
0 new messages