'substr' function

322 views
Skip to first unread message

Jonathan Gazeley

unread,
Sep 4, 2014, 5:20:17 AM9/4/14
to puppet...@googlegroups.com
Another dumb question, but I can't find what I'm looking for. Does
Puppet have a function that's equivalent to the substr() function in
Perl? I want to truncate arbitrary strings to 7 chars but I can't see
how to do this with any of the built-in functions or functions in stdlib.

e.g. substr('hello', 4) returns 'hell'

Thanks,
Jonathan

Martin Alfke

unread,
Sep 4, 2014, 5:28:47 AM9/4/14
to puppet...@googlegroups.com
I assume that you can use the sprintf function (from Puppet core).

sprintf("%20.8s", "string test") #=> " string t”
> --
> You received this message because you are subscribed to the Google Groups "Puppet Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/54082EBE.4020509%40bristol.ac.uk.
> For more options, visit https://groups.google.com/d/optout.

Jonathan Gazeley

unread,
Sep 4, 2014, 5:39:25 AM9/4/14
to puppet...@googlegroups.com
Apparently it is also possible with regsubst, like this:

regsubst($hostname, '^(.{7})(.*)', '\1')
Reply all
Reply to author
Forward
0 new messages