4 module Puppet::Parser::Functions
5 newfunction(:delete, :type => :rvalue, :doc => <<-DOC
7 Deletes all instances of a given element from an array, substring from a
8 string, or key from a hash.
10 @example Example usage
12 delete(['a','b','c','b'], 'b')
13 Would return: ['a','c']
15 delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
16 Would return: {'a'=>1,'c'=>3}
18 delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
19 Would return: {'a'=>1}
21 delete('abracadabra', 'bra')
24 ['a', 'b', 'c', 'b'] - 'b'
25 Would return: ['a', 'c']
27 {'a'=>1,'b'=>2,'c'=>3} - ['b','c'])
28 Would return: {'a' => '1'}
30 'abracadabra'.regsubst(/bra/, '', 'G')
34 From Puppet 4.0.0 the minus (-) operator deletes values from arrays and keys from a hash
35 `{'a'=>1,'b'=>2,'c'=>3} - ['b','c'])`
37 A global delete from a string can be performed with the
38 [`regsubst`](https://puppet.com/docs/puppet/latest/function.html#regsubst) function:
39 `'abracadabra'.regsubst(/bra/, '', 'G')`
41 In general, the built-in [`filter`](https://puppet.com/docs/puppet/latest/function.html#filter)
42 function can filter out entries from arrays and hashes based on keys and/or values.
44 @return [String] The filtered String, if one was given.
45 @return [Hash] The filtered Hash, if one was given.
46 @return [Array] The filtered Array, if one was given.
50 raise(Puppet::ParseError, "delete(): Wrong number of arguments given #{arguments.size} for 2") unless arguments.size == 2
52 collection = arguments[0].dup
53 Array(arguments[1]).each do |item|
56 collection.delete item
58 collection.gsub! item, ''
60 raise(TypeError, "delete(): First argument must be an Array, String, or Hash. Given an argument of class #{collection.class}.")
67 # vim: set ts=2 sw=2 et :