JeremyCampbell
2012-Sep-10 17:22 UTC
[Puppet Users] How to remove last comma when iterating through hash in erb template
I need to produce a line in a config file in the format x = "ip1,ip2,ip3" I am using the method below to sort the hash before iterating over it. However, as you can see there will always be a final comma which breaks the app that uses this config file. Does anyone know how I could remove the final comma? ipv4_bind_addresses = "<% routes.sort_by {|key, v| key}.each do |key, v| -%><%= net %>.<%= v[''pubnet''] %>.1,<% end -%>" -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/DsCB3zXfHk0J. 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.
Mason Turner
2012-Sep-10 17:24 UTC
Re: [Puppet Users] How to remove last comma when iterating through hash in erb template
I use this: <%= relay_destinations.join('','') %> On Sep 10, 2012, at 1:22 PM, JeremyCampbell <jeremycampbell87@gmail.com> wrote:> I need to produce a line in a config file in the format x = "ip1,ip2,ip3" > > I am using the method below to sort the hash before iterating over it. However, as you can see there will always be a final comma which breaks the app that uses this config file. Does anyone know how I could remove the final comma? > > ipv4_bind_addresses = "<% routes.sort_by {|key, v| key}.each do |key, v| -%><%= net %>.<%= v[''pubnet''] %>.1,<% end -%>" > > > -- > You received this message because you are subscribed to the Google Groups "Puppet Users" group. > To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/DsCB3zXfHk0J. > 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.-- 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-Sep-10 17:28 UTC
Re: [Puppet Users] How to remove last comma when iterating through hash in erb template
----- Original Message -----> From: "JeremyCampbell" <jeremycampbell87@gmail.com> > To: puppet-users@googlegroups.com > Sent: Monday, September 10, 2012 6:22:17 PM > Subject: [Puppet Users] How to remove last comma when iterating through hash in erb template > > I need to produce a line in a config file in the format x > "ip1,ip2,ip3" > > I am using the method below to sort the hash before iterating over > it. However, as you can see there will always be a final comma which > breaks the app that uses this config file. Does anyone know how I > could remove the final comma? > > ipv4_bind_addresses = "<% routes.sort_by {|key, v| key}.each do |key, > v| -%><%= net %>.<%= v[''pubnet''] %>.1,<% end -%>"the key is to go via an array and then use join: ipv4_bind_addresses = "<%= routes.sort_by {|key, v| key}.map{|key, v| ''%s.%s.1'' % [net, v[''pubnet'']]}.join('','') -%>" not seeing your data so it''s hard to test if this will work for you but the ''map'' method takes the block and returns an array of each iteration of the block - strings in this case. Calling join on the strings will then avoid the trailing , -- 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.
JeremyCampbell
2012-Sep-11 07:26 UTC
Re: [Puppet Users] How to remove last comma when iterating through hash in erb template
On Monday, September 10, 2012 7:28:58 PM UTC+2, R.I. Pienaar wrote:> > > > ----- Original Message ----- > > From: "JeremyCampbell" <jeremyca...@gmail.com <javascript:>> > > To: puppet...@googlegroups.com <javascript:> > > Sent: Monday, September 10, 2012 6:22:17 PM > > Subject: [Puppet Users] How to remove last comma when iterating through > hash in erb template > > > > I need to produce a line in a config file in the format x = > > "ip1,ip2,ip3" > > > > I am using the method below to sort the hash before iterating over > > it. However, as you can see there will always be a final comma which > > breaks the app that uses this config file. Does anyone know how I > > could remove the final comma? > > > > ipv4_bind_addresses = "<% routes.sort_by {|key, v| key}.each do |key, > > v| -%><%= net %>.<%= v[''pubnet''] %>.1,<% end -%>" > > > the key is to go via an array and then use join: > > ipv4_bind_addresses = "<%= routes.sort_by {|key, v| key}.map{|key, v| > ''%s.%s.1'' % [net, v[''pubnet'']]}.join('','') -%>" > > not seeing your data so it''s hard to test if this will work for you but >> the ''map'' method takes the block and returns an array of each iteration >> of the block - strings in this case. Calling join on the strings will >> then avoid the trailing , >> > Thats worked perfectly! Dankie meneer!-- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To view this discussion on the web visit https://groups.google.com/d/msg/puppet-users/-/ORDDDpMGpeIJ. 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.