#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    12

    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
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    /usr/bin/ruby
    Posts
    63
    Rep Power
    29
    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 <<
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2004
    Location
    Constant Limbo
    Posts
    989
    Rep Power
    363
    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?
    True happiness is not getting what you want, it's wanting what you've already got.

    My Blog
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2007
    Location
    /usr/bin/ruby
    Posts
    63
    Rep Power
    29
    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
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    12
    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
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Nov 2003
    Location
    Edinburgh, UK
    Posts
    84
    Rep Power
    12
    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 }}

IMN logo majestic logo threadwatch logo seochat tools logo