Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.ruby > #2951 > unrolled thread

Extract a range i.e. svr[100..130] ?

Started byRichard Sandoval <skolopen@yahoo.com>
First post2011-04-15 11:08 -0500
Last post2011-04-15 17:03 -0500
Articles 13 — 3 participants

Back to article view | Back to comp.lang.ruby


Contents

  Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 11:08 -0500
    Re: Extract a range i.e. svr[100..130] ? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 11:41 -0500
      Re: Extract a range i.e. svr? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-15 12:36 -0500
    Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 13:24 -0500
      Re: Extract a range i.e. svr[100..130] ? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-15 13:27 -0500
    Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 13:40 -0500
      Re: Extract a range i.e. svr[100..130] ? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 15:50 -0500
    Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 13:46 -0500
    Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 15:11 -0500
      Re: Extract a range i.e. svr[100..130] ? 7stud -- <bbxx789_05ss@yahoo.com> - 2011-04-18 15:33 -0500
    Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 16:48 -0500
    Re: Extract a range i.e. svr[100..130] ? Richard Sandoval <skolopen@yahoo.com> - 2011-04-15 16:54 -0500
      Re: Extract a range i.e. svr[100..130] ? Jesús Gabriel y Galán <jgabrielygalan@gmail.com> - 2011-04-15 17:03 -0500

#2951 — Extract a range i.e. svr[100..130] ?

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 11:08 -0500
SubjectExtract a range i.e. svr[100..130] ?
Message-ID<a09ed402a715adbb77804d95cdf94bf2@ruby-forum.com>
What best method could extract the range of a given list of servers?

I have a field name on a UI that contains a list of servers and it can
be a range such as
svr[100..130].domain.local,prod[10..13].otherdomain.local.


What would be my best approach to single each one of those nodes out to
iterate through?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [next] | [standalone]


#2953

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-15 11:41 -0500
Message-ID<BANLkTikF=HrdQnu9DQTnp-Buzr6dtnMjLQ@mail.gmail.com>
In reply to#2951
On Fri, Apr 15, 2011 at 6:08 PM, Richard Sandoval <skolopen@yahoo.com> wrote:
> What best method could extract the range of a given list of servers?
>
> I have a field name on a UI that contains a list of servers and it can
> be a range such as
> svr[100..130].domain.local,prod[10..13].otherdomain.local.
>
>
> What would be my best approach to single each one of those nodes out to
> iterate through?

Sorry, if I didn't understand this well. You have a string containing
"svr[100..130].domain.local" and you want:

svr100.domain.local
svr101.domain.local
...
svr130.domain.local

?

If that's the case, then this might work:

a = "svr[100..130].domain.local"
m = a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)
(m[2].to_i..m[3].to_i).each {|num| puts "#{m[1]}#{num}#{m[4]}"}

=>
svr100.domain.local
svr101.domain.local
svr102.domain.local
[...snip...]
svr129.domain.local
svr130.domain.local

Jesus

[toc] | [prev] | [next] | [standalone]


#2958 — Re: Extract a range i.e. svr?

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-15 12:36 -0500
SubjectRe: Extract a range i.e. svr?
Message-ID<179b1d9eb1df11dd8b91b81c3a6faabb@ruby-forum.com>
In reply to#2953
"Jesús Gabriel y Galán" <jgabrielygalan@gmail.com> wrote in post 
#993035:
> Sorry, if I didn't understand this well. You have a string containing
> "svr[100..130].domain.local" and you want:
>
> svr100.domain.local
> svr101.domain.local
> ....
> svr130.domain.local
>
> ?
>
> If that's the case, then this might work:
>
> a = "svr[100..130].domain.local"
> m = a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)
> (m[2].to_i..m[3].to_i).each {|num| puts "#{m[1]}#{num}#{m[4]}"}
>

Here's my version:

str = "svr[100..130].domain.local"

range_pattern = /
    \[       #a literal opening bracket
    (\d+)    #capture a series of one or more digits
    [.]{2}   #two literal periods
    (\d+)    #capture a series of one or more digits
    \]       #a literal closing bracket
/xms

before_range, the_range, after_range = str.partition(range_pattern)
start_range, end_range = $1, $2

start_range.upto(end_range) do |i|
  puts "#{before_range}#{i}#{after_range}"
end

--output:--
svr100.domain.local
svr101.domain.local
svr102.domain.local
svr103.domain.local
svr104.domain.local
svr105.domain.local
svr106.domain.local
svr107.domain.local
svr108.domain.local
svr109.domain.local
svr110.domain.local
svr111.domain.local
svr112.domain.local
svr113.domain.local
svr114.domain.local
svr115.domain.local
svr116.domain.local
svr117.domain.local
svr118.domain.local
svr119.domain.local
svr120.domain.local
svr121.domain.local
svr122.domain.local
svr123.domain.local
svr124.domain.local
svr125.domain.local
svr126.domain.local
svr127.domain.local
svr128.domain.local
svr129.domain.local
svr130.domain.local

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2964

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 13:24 -0500
Message-ID<87fcf161a223a9c5fea84a7f67361cb7@ruby-forum.com>
In reply to#2951
This is a definite step in the right direction and I appreciate your 
assistance Jesus.

So I have a fieldname in a UI which is named Hostnames:

Within the hostname field, it could have a single host named 
svr10.domain.local or it could have a range like 
svr.10.domain.local,svr[100..103].domain.local.

Essentially what I am trying to do is to get that hostname field in my 
script and for each individual host and/or a range of hosts then do a 
specific command or go through my work flow.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2965

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-15 13:27 -0500
Message-ID<56bbff0dbb695a0b2f6c74887d847c11@ruby-forum.com>
In reply to#2964
Richard Sandoval wrote in post #993057:
> This is a definite step in the right direction and I appreciate your
> assistance Jesus/7stud
>
> So I have a fieldname in a UI which is named Hostnames:
>
> Within the hostname field, it could have a single host named
> svr10.domain.local or it could have a range like
> svr.10.domain.local,svr[100..103].domain.local.
>
> Essentially what I am trying to do is to get that hostname field in my
> script and for each individual host and/or a range of hosts then do a
> specific command or go through my work flow.

..and so what have you tried given the above?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2967

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 13:40 -0500
Message-ID<0f3edb89579b39aedc7026efee11507e@ruby-forum.com>
In reply to#2951
undefined method `match' for ["svr[100..103].domain.local"]:Array 
(NoMethodError)

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2978

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-15 15:50 -0500
Message-ID<BANLkTimBwXZ6wMprqvWjX_CHdT6hjVcDYQ@mail.gmail.com>
In reply to#2967
On Fri, Apr 15, 2011 at 8:40 PM, Richard Sandoval <skolopen@yahoo.com> wrote:
> undefined method `match' for ["svr[100..103].domain.local"]:Array
> (NoMethodError)

This is because you don't have a string, you have an array. If you
have that value for example in params[:hostname], try this:

a = params[:hostname].first

and then the rest of my solution.

Jesus.

[toc] | [prev] | [next] | [standalone]


#2970

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 13:46 -0500
Message-ID<24d911aa04c478fa87868a770951e0d4@ruby-forum.com>
In reply to#2951
it might be that my loop is wrong, ill investigate further, thank you 
again.

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2976

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 15:11 -0500
Message-ID<d8e2dd7395460e7392ed7e5710d3d226@ruby-forum.com>
In reply to#2951
7stud,

you use upto but that doesnt work for 1.8.6, what method could I use in 
this scenario?

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#3119

From7stud -- <bbxx789_05ss@yahoo.com>
Date2011-04-18 15:33 -0500
Message-ID<086acb9ca822a50a638c82458c32f768@ruby-forum.com>
In reply to#2976
Richard Sandoval wrote in post #993091:
> 7stud,
>
> you use upto but that doesnt work for 1.8.6, what method could I use in
> this scenario?

puts RUBY_VERSION

1.upto(5) do |i|
  puts i
end

--output:--
1.8.6
1
2
3
4
5

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2980

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 16:48 -0500
Message-ID<3ea0b71727c5ea7d56e57e5f03550189@ruby-forum.com>
In reply to#2951
Hi jesus, svr[100..103].domain.local would be one of the keys in the 
array.

My array looks like

["svr10.domain.local", "svr[100-103].domain.local", 
"svr[200-300].domain.local"]

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2981

FromRichard Sandoval <skolopen@yahoo.com>
Date2011-04-15 16:54 -0500
Message-ID<5a352e9543472f6edc07e1e5640a1fd8@ruby-forum.com>
In reply to#2951
Here is what i am trying to do.



nodes = cfv.values.to_s.split(/[, \n]+/)
 a = nodes
 m = a.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)

 puts "nodes: #{m.inspect}"


m.each  do |node|
puts "checking to see if #{node} exists"

-- 
Posted via http://www.ruby-forum.com/.

[toc] | [prev] | [next] | [standalone]


#2982

FromJesús Gabriel y Galán <jgabrielygalan@gmail.com>
Date2011-04-15 17:03 -0500
Message-ID<BANLkTimah3qhBCrjMUmpGDaEXNm-zQ25Bg@mail.gmail.com>
In reply to#2981
On Fri, Apr 15, 2011 at 11:54 PM, Richard Sandoval <skolopen@yahoo.com> wrote:
> Here is what i am trying to do.
>
>
>
> nodes = cfv.values.to_s.split(/[, \n]+/)

split returns an array
(http://ruby-doc.org/core-1.8.7/classes/String.html#M000776). So you
can do the following:

nodes.each do |node|
  m = node.match(/(.*?)\[(\d+)\.\.(\d+)\](.*)/)
  if m
    (m[2].to_i..m[3].to_i).each {|num|
do_something_with("#{m[1]}#{num}#{m[4]}")}
  else
    do_something_with(node)
  end
end

This will call do_something_with passing either each expanded server
name or the original string if it doesn't match the regular
expression.

Jesus.

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.ruby


csiph-web