# Help with array

**URL:** <https://www.ruby-forum.com/t/help-with-array/143339>\
**Category:** Ruby\
**Created:** [July 17, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/help-with-array/143339 "2008-07-17T15:21:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 17, 2008, 3:21pm UTC](https://www.ruby-forum.com/t/help-with-array/143339/1 "2008-07-17T15:21:18Z")

</div>

Hello team,

I am having an issue with the code below.  
Could you please take a quick look at the highlighted and commented code  
below and let me know what’s wrong?

Thank you

Victor

input.file  
group.was=hostname1 hostname.2 hostname.r3  
group.web=webserver1 webserver2 webserver3 hostname1  
/usr/local/bin/test.sh  
web /usr/local/bin/testcmd.sh  
#end of input.file

# Program

@myhostname = `hostname` # webserver3  
#comment# The intent is to create a arrays “was” and “web” and to assign  
the  
values in the group statement  
IO.foreach(“inpit.file”) do |line|  
if line =~ /^#/ || line.chomp.length == 0  
next  
end # if line  
if line.upcase =~ /^GROUP/  
grouplit, groupinfo = line.split(".")  
groupname, groupentries = groupinfo.split("=")  
#{groupname} = groupname.to\_a  
groupname = groupentries.split  
next  
end # if GROUP  
targetserver, command = line.split(" ")  
if targetserver == @myhostname  
@runcommand = true  
end # if targetserver  
#comment# In this case I’m trying to determine if “myhostname” is in the  
array “web”  
#comment# I’ve tried a number of ways to get the following to work  
without  
success  
if #{targetserver}._include?_ “#{@myhostname %23{@myhostname}”  
####### THIS IS WHERE THE FAILURE IS  
@runcommand = true  
end # if  
end  
exit

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 17, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/help-with-array/143339/2 "2008-07-17T15:50:21Z")

</div>

On Thu, Jul 17, 2008 at 3:15 PM, Victor R. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> IO.foreach(“inpit.file”) do |line|  
> targetserver, command = line.split(" ")  
> end # if  
> end  
> exit

Try to parse this code

if  
@runcommand = true  
end

Do you see what is wrong?  
If not scroll down 😉

The escape #{ works in string literals only, so you probably just  
wanted to write

if targetserver.include? “#{@myhostname %23{@myhostname}”

I have no idea however what you meant with _.include?_ (just high  
lightening, I guess).

HTH  
Robert

–  
[http://ruby-smalltalk.blogspot.com/](http://ruby-smalltalk.blogspot.com/)

* * *

AALST (n.) One who changes his name to be further to the front  
D.Adams; The Meaning of LIFF

---

<div class="post-metadata">

**Author:** ![ribes](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/f4b2a3/32.png) [@ribes](https://www.ruby-forum.com/u/ribes)\
**Post date:** [July 17, 2008, 3:50pm UTC](https://www.ruby-forum.com/t/help-with-array/143339/3 "2008-07-17T15:50:56Z")

</div>

Victor R. wrote:

> IO.foreach(“inpit.file”) do |line|  
> targetserver, command = line.split(" ")  
> end # if  
> end  
> exit

Victor,

In ruby, a statement like

#{foo} = ‘bar’

just won’t work. You are trying to use “dynamic variables” (ala PHP I  
guess) and that simply does not work in ruby. Here is a version of your  
code that I think does what you are trying to do. I removed the @  
sigils on the variable names … you might need to add them back if you  
took this code out of a class you are working on. I also stored the  
group arrays in a ruby Hash object keyed by the group name. There are  
other ways to do this, the best one depends on your specific needs. You  
also had what looked to be a misspelling in the IO.foreach statement:  
your version was opening ‘inpit.file’ not ‘input.file’.

# Program

#@myhostname = `hostname` # webserver3  
myhostname = ‘webserver3’  
runcommand = false  
command\_to\_run = nil  
#comment# The intent is to create a arrays “was” and “web” and to assign  
the values in the group statement  
groups = {}  
IO.foreach(“input.file”) do |line|  
if line =~ /^#/ || line.chomp.length == 0  
next  
end # if line  
if line.upcase =~ /^GROUP/  
grouplit, groupinfo = line.split(".")  
groupname, groupentries = groupinfo.split("=")  
groups[groupname] = groupentries.split  
next  
end # if GROUP  
targetserver, command\_to\_run = line.split(" ")  
if targetserver == myhostname  
runcommand = true  
end # if targetserver  
#comment# In this case I’m trying to determine if “myhostname” is in the  
array “web”  
#comment# I’ve tried a number of ways to get the following to work  
without success  
end

if groups[‘web’].include?(myhostname)  
####### THIS IS WHERE THE FAILURE IS  
runcommand = true  
end  
puts “groups: #{groups.inspect}”  
puts “run command: #{runcommand}, command: #{command\_to\_run}”  
exit
