James Patterson
2012-Jun-20 21:39 UTC
[Puppet Users] How to iterate over an array? (with a counter)
Given an array, I''d like to iterate over it with a counter to produce something like this from a template: foo.0 = ARRAYVALUE0 foo.1 = ARRAYVALUE1 foo.2 = ARRAYVALUE2 (where ARRAYVALUEn is replaced by the value, and the foo.n is incremented for each value) The puppet templates support iteration, but I don''t see how to also increment a counter to get the behaviour above? Is this possible? Thanks. -- James Patterson jamespatterson@operamail.com -- http://www.fastmail.fm - Or how I learned to stop worrying and love email again -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
R.I.Pienaar
2012-Jun-20 21:42 UTC
Re: [Puppet Users] How to iterate over an array? (with a counter)
----- Original Message -----> From: "James Patterson" <jamespatterson@operamail.com> > To: puppet-users@googlegroups.com > Sent: Wednesday, June 20, 2012 10:39:02 PM > Subject: [Puppet Users] How to iterate over an array? (with a counter) > > Given an array, I''d like to iterate over it with a counter to produce > something like this from a template: > > foo.0 = ARRAYVALUE0 > foo.1 = ARRAYVALUE1 > foo.2 = ARRAYVALUE2 > > (where ARRAYVALUEn is replaced by the value, and the foo.n is > incremented for each value) > > The puppet templates support iteration, but I don''t see how to also > increment a counter to get the behaviour above? Is this possible? > Thanks.array.each_with_index do |item, index| ... end item will be each item and index will increment from 0 upward -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
James Patterson
2012-Jun-21 06:31 UTC
Re: [Puppet Users] How to iterate over an array? (with a counter)
On Wed, Jun 20, 2012, at 10:42 PM, R.I.Pienaar wrote:> > > ----- Original Message ----- > > From: "James Patterson" <jamespatterson@operamail.com> > > To: puppet-users@googlegroups.com > > Sent: Wednesday, June 20, 2012 10:39:02 PM > > Subject: [Puppet Users] How to iterate over an array? (with a counter) > > > > Given an array, I''d like to iterate over it with a counter to produce > > something like this from a template: > > > > foo.0 = ARRAYVALUE0 > > foo.1 = ARRAYVALUE1 > > foo.2 = ARRAYVALUE2 > > > > (where ARRAYVALUEn is replaced by the value, and the foo.n is > > incremented for each value) > > > > The puppet templates support iteration, but I don''t see how to also > > increment a counter to get the behaviour above? Is this possible? > > Thanks. > > array.each_with_index do |item, index| > ... > end > > item will be each item and index will increment from 0 upward >Excellent stuff! Just what I wanted! If I wanted to sort numerically, backwards, skip numbers, do ($i=0; $i<100; $i+2), etc, where should I read about that? Thanks. -- http://www.fastmail.fm - Does exactly what it says on the tin -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
R.I.Pienaar
2012-Jun-21 08:36 UTC
Re: [Puppet Users] How to iterate over an array? (with a counter)
----- Original Message -----> From: "James Patterson" <jamespatterson@operamail.com> > To: puppet-users@googlegroups.com > Sent: Thursday, June 21, 2012 7:31:58 AM > Subject: Re: [Puppet Users] How to iterate over an array? (with a counter) > > On Wed, Jun 20, 2012, at 10:42 PM, R.I.Pienaar wrote: > > > > > > ----- Original Message ----- > > > From: "James Patterson" <jamespatterson@operamail.com> > > > To: puppet-users@googlegroups.com > > > Sent: Wednesday, June 20, 2012 10:39:02 PM > > > Subject: [Puppet Users] How to iterate over an array? (with a > > > counter) > > > > > > Given an array, I''d like to iterate over it with a counter to > > > produce > > > something like this from a template: > > > > > > foo.0 = ARRAYVALUE0 > > > foo.1 = ARRAYVALUE1 > > > foo.2 = ARRAYVALUE2 > > > > > > (where ARRAYVALUEn is replaced by the value, and the foo.n is > > > incremented for each value) > > > > > > The puppet templates support iteration, but I don''t see how to > > > also > > > increment a counter to get the behaviour above? Is this possible? > > > Thanks. > > > > array.each_with_index do |item, index| > > ... > > end > > > > item will be each item and index will increment from 0 upward > > > > Excellent stuff! Just what I wanted! > > If I wanted to sort numerically, backwards, skip numbers, do ($i=0; > $i<100; $i+2), etc, where should I read about that?The best place to look is the ruby docs for Array[1] and Enumberable[2] these are the methods available to you. Getting used to how ruby does loops and stuff can be a bit weird, it has a for loop but not one that lets you easily tweak the itteration count etc, you''d probably do that with a while or until for best readability to people who dont know ruby very well. http://www.tutorialspoint.com/ruby/ruby_loops.htm [1] http://www.ruby-doc.org/core-1.9.3/Array.html [2] http://www.ruby-doc.org/core-1.9.3/Enumerable.html -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To post to this group, send email to puppet-users@googlegroups.com. To unsubscribe from this group, send email to puppet-users+unsubscribe@googlegroups.com. For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.