> I'm working on a provider and it needs to parce the output from a
> command that's space separated on a single line. So far I've been
> unsuccessful at splitting it up so I can loop thru the values to see
> if a certain value is present, my utter lack of programming exp isn't
> helping things. How can I split the output?
A few things that might be helpful:
1. If you haven't already, check out irb, a command line utility that
lets you try out little bits of ruby.
2. String#split(regex) is likely what you are looking for.
my_line.split(/ +/)
will split the string on each occurrence of one or more spaces.
3. Array#any? {test} may also be useful. It returns true if any of the
elements in the array pass the test.
Try going in to irb and playing with these to see if they do what you
want:
> irb
irb(main):001:0> "This is a test".split(/ +/)
=> ["This", "is", "a", "test"]
irb(main):002:0> "This is a test".split(/ +/).any? { |w| w == 'a' }
=> true
irb(main):003:0> "This is a test".split(/ +/).any? { |w| w == 'an' }
=> false
4. A more direct solution may be to use a regular expression to test for
the value you are interested in directly, without bothering to split up
the line. By using a \b at the start and end of your regular expression
you can make it only match on word boundaries:
irb(main):004:0> "This is a test" =~ /\bhis\b/i
=> nil
irb(main):005:0> "This is a test" =~ /\bthis\b/i
=> 0
(Note that 0, meaning the start of the string, counts as true in ruby,
while nil, meaning no match found, counts as false).
-- Markus
--
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-dev?hl=en.