Puppet – Split – Get last element of variable size array

puppet

Anyone know a clever way to get the last element of an array within a puppet manifest?

Existing code looks like:

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?

Best Answer

Arrays support negative indexing, with -1 being the final element of the array:

Link to documentation

so..

$foo = [ 'one', 'two', 'three', 'four', 'five' ]
notice( $foo[-1] )
# 'five'
Related Topic