#
# grep.rb
#
-
module Puppet::Parser::Functions
- newfunction(:grep, :type => :rvalue, :doc => <<-EOS
-This function searches through an array and returns any elements that match
-the provided regular expression.
+ newfunction(:grep, :type => :rvalue, :doc => <<-DOC
+ This function searches through an array and returns any elements that match
+ the provided regular expression.
+
+ *Examples:*
-*Examples:*
+ grep(['aaa','bbb','ccc','aaaddd'], 'aaa')
- grep(['aaa','bbb','ccc','aaaddd'], 'aaa')
+ Would return:
-Would return:
+ ['aaa','aaaddd']
- ['aaa','aaaddd']
- EOS
- ) do |arguments|
+ Note that since Puppet 4.0.0, the filter() function in Puppet can do the same:
- if (arguments.size != 2) then
+ ['aaa', 'bbb', 'ccc', 'aaaddd']. filter |$x| { $x =~ 'aaa' }
+ DOC
+ ) do |arguments|
+
+ if arguments.size != 2
raise(Puppet::ParseError, "grep(): Wrong number of arguments given #{arguments.size} for 2")
end
pattern = Regexp.new(arguments[1])
a.grep(pattern)
-
end
end