IBM developerWorks
           Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
1200+ fellow developers rate and compare features of the top IDEs, like Visual Studio, Eclipse, RAD, Delphi and others, across 13 categories. Enjoy this FREE Download of the IDE User Satisfaction Study by Evans Data Corporation. Download Now!
  #1  
Old October 16th, 2007, 11:37 AM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 71 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 37 m 34 sec
Reputation Power: 5
Send a message via MSN to karym6
Concatenating array item with string

Hi All,

I have a (possibly) unique problem, I have an array of values that I would like to concatenate with a string.

eg;

servers=['server1','server2','server2']
path = "/some_path"

I would like to be able to concatenate server[i] with path in order to create a complete path. I have tried server[i] + path, however ruby doesnt allow this at all.

Can some one let me know if this is possible at all?

Cheers
-
Karym6

Reply With Quote
  #2  
Old October 16th, 2007, 11:59 AM
[H4z3] [H4z3] is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: /usr/bin/ruby
Posts: 63 [H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 32 m 36 sec
Reputation Power: 23
Code:
servers = ["server1", "server2", "server3"]
path = "/"
servers.each { |s| path << s }
 
# path => /server1server2server3


Or perhaps you mean..

Code:
servers = ["server1", "server2", "server3"]
path = "/"
servers.each { |s| path << s + "/" }
# path => /server1/server2/server3/


You can use += .. or <<

Reply With Quote
  #3  
Old October 16th, 2007, 12:05 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 591 L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level)L7Sqr User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 13 h 59 m 46 sec
Reputation Power: 100
Send a message via AIM to L7Sqr
Code:
irb(main):001:0> s = ['server1', 'server2', 'server3']
=> ["server1", "server2", "server3"]
irb(main):002:0> pth = "/path"
=> "/path"
irb(main):003:0> s.collect { |i| pth + "/" + i }.each { |i| puts i }
/path/server1
/path/server2
/path/server3
=> ["/path/server1", "/path/server2", "/path/server3"]
irb(main):004:0> s.collect { |i| i + pth }.each { |i| puts i }
server1/path
server2/path
server3/path
=> ["server1/path", "server2/path", "server3/path"]
irb(main):005:0> 

I'm not sure what isnt working for you... Could you post a snippet of teh problem area so that we may see that it is not a syntax error?
__________________
-- I'll provide you with reference points; if they dont work, refer to something else.

If you process text, this might make your life a little easier.

Reply With Quote
  #4  
Old October 16th, 2007, 12:13 PM
[H4z3] [H4z3] is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: /usr/bin/ruby
Posts: 63 [H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level)[H4z3] User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 20 h 32 m 36 sec
Reputation Power: 23
Oh, I was thinking he wanted to make a complete path from all of the values..

+ is the concatenation operator

<< is combined assignment and concatenation

Reply With Quote
  #5  
Old October 16th, 2007, 12:57 PM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 71 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 37 m 34 sec
Reputation Power: 5
Send a message via MSN to karym6
Code:
puts "Checking to see if all sites have home pages"
puts "Now checking..."
servers = ['server1','server2','server3','server4','server5']
office_urls = CSV.read 'C:\\ReleaseScripts\\tests\\officeURLs.csv'
rpb_urls = CSV.read 'C:\\ReleaseScripts\\tests\\Burls.csv'
supermarket_urls = CSV.read 'C:\\ReleaseScripts\\tests\\marketURLs.csv'
businessclub_urls = CSV.read 'C:\\ReleaseScripts\\tests\\ClubURLs.csv'
office_urls.each {|path| 
servers.each  |s| path << s + "URL" 
puts path
}
puts "============================================================"


currently, it doesnt work as you will see - I have been trying out some of the suggestions above.

Ideally, I would like to have <server><path> printed on one line.

Cheers
-
Karym6

Reply With Quote
  #6  
Old October 16th, 2007, 01:09 PM
karym6 karym6 is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2003
Location: Edinburgh, UK
Posts: 71 karym6 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 h 37 m 34 sec
Reputation Power: 5
Send a message via MSN to karym6
Guys, cracked it - coudnt have done it with out your guidance

I acheived my aim with:

Code:
office_urls.each {|path| servers.collect { |i| i + "#{path}" }.each { |i| puts i }}

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > Concatenating array item with string


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway