任何人都知道一个聪明的方法来获取木偶清单中的数组的最后一个元素?
现有的代码如下所示:
class nginx { define vhost { #----- # Init vars #----- $app_parts = split($name, '[_]') # I can access any element using numeric notation notify { "Element: ${app_parts[0]}": } # How do I access the last element?
Arrays support negative indexing, with -1 being the final element of the array:
http://docs.puppetlabs.com/puppet/2.7/reference/lang_datatypes.html#indexing
所以..
$foo = [ 'one', 'two', 'three', 'four', 'five' ] notice( $foo[-1] ) # 'five'