3

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?

1 Answer 1

6
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'
2
  • Ugh, I should have guessed that one. This worked, thanks! Apr 26, 2013 at 20:22
  • S'ok. I had to test the theory out in IRB, anyway, then went off googling. Apr 26, 2013 at 20:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .