4 module Puppet::Parser::Functions
5 newfunction(:delete, :type => :rvalue, :doc => <<-DOC
6 Deletes all instances of a given element from an array, substring from a
7 string, or key from a hash.
11 delete(['a','b','c','b'], 'b')
12 Would return: ['a','c']
14 delete({'a'=>1,'b'=>2,'c'=>3}, 'b')
15 Would return: {'a'=>1,'c'=>3}
17 delete({'a'=>1,'b'=>2,'c'=>3}, ['b','c'])
18 Would return: {'a'=>1}
20 delete('abracadabra', 'bra')
23 Note that from Puppet 4.0.0 the minus (-) operator deletes values from arrays and keys from a hash:
25 ['a', 'b', 'c', 'b'] - 'b'
26 # would return ['a', 'c']
28 {'a'=>1,'b'=>2,'c'=>3} - ['b','c'])
29 # would return {'a' => '1'}
31 A global delete from a string can be performed with the regsubst() function:
33 'abracadabra'.regsubst(/bra/, '', 'G')
34 # would return 'acada'
36 In general, the filter() function can filter out entries from arrays and hashes based on keys and/or values.
41 raise(Puppet::ParseError, "delete(): Wrong number of arguments given #{arguments.size} for 2") unless arguments.size == 2
43 collection = arguments[0].dup
44 Array(arguments[1]).each do |item|
47 collection.delete item
49 collection.gsub! item, ''
51 raise(TypeError, "delete(): First argument must be an Array, String, or Hash. Given an argument of class #{collection.class}.")
58 # vim: set ts=2 sw=2 et :