4 module Puppet::Parser::Functions
5 newfunction(:delete_at, :type => :rvalue, :doc => <<-DOC) do |arguments|
7 Deletes a determined indexed value from an array.
10 ```delete_at(['a','b','c'], 1)```
12 Would return: `['a','c']`
15 Since Puppet 4 this can be done in general with the built-in
16 [`filter`](https://puppet.com/docs/puppet/latest/function.html#filter) function:
18 ```['a', 'b', 'c'].filter |$pos, $val | { $pos != 1 }```
20 Or if a delete is wanted from the beginning or end of the array, by using the slice operator [ ]:
22 $array[0, -1] # the same as all the values
23 $array[2, -1] # all but the first 2 elements
24 $array[0, -3] # all but the last 2 elements
25 $array[1, -2] # all but the first and last element
28 @return [Array] The given array, now missing the target value
32 raise(Puppet::ParseError, "delete_at(): Wrong number of arguments given (#{arguments.size} for 2)") if arguments.size < 2
36 unless array.is_a?(Array)
37 raise(Puppet::ParseError, 'delete_at(): Requires array to work with')
42 if index.is_a?(String) && !index.match(%r{^\d+$})
43 raise(Puppet::ParseError, 'delete_at(): You must provide non-negative numeric index')
48 # Numbers in Puppet are often string-encoded which is troublesome ...
51 if index > result.size - 1 # First element is at index 0 is it not?
52 raise(Puppet::ParseError, 'delete_at(): Given index exceeds size of array given')
55 result.delete_at(index) # We ignore the element that got deleted ...
61 # vim: set ts=2 sw=2 et :