0
String = "Mod1:10022932,10828075,5946410,13321905,5491120,5030731|Mod2:22704455,22991440,22991464,21984312,21777721,21777723,21889761,21939852,23091478,22339903,23091485,22099714,21998260,22364832,21939858,21944274,21944226,22800221,22704443,21777728,21777719,21678184,21998265,21834900,21984331,22704454,21998261,21944214,21862610,21836482|Mod3:10828075,13321905,5491120,5946410,5030731,15806212,4100566,4787137,2625339,2408317,2646868,19612047,2646862,11983534,8591489,19612048,10249319,14220471,15806209,13330887,15075124,17656842,3056657,5086273|Mod4:10828075,5946410,13321905,5030731,5491120,4787137,4100566,15806212,2625339,3542205,2408317,2646862,2646868|Mod5:10022932;0.2512,10828075;0.2093,5030731;0.1465,5946410;0.1465,4787137;0.1465,2625339;0.0143,5491120;0.0143,13321905;0.0143,3542205;0.0143,15806212;0.0119,4100566;0.0119,19612047;0.0100,2408317;0.0100"

How can I split it out so that I can get each title(Mod1, Mod2..) and the ID's that belong to each title.

This is that I've tried so far, which is removing everything after the pipe, which I dont want.

mod_name = string.split(":")[0]
mod_ids = string.split(":")[1] #This gets me the ID's but also include the |Mod*
ids = mod_mod_ids.split("|").first.strip #Only returns Id's before the first "|"

Desired Output: I need to save mod_name and mod_ids to their respective columns,

mod_name = #name ("Mod1...Mod2 etc) #string
mod_ids = #ids (All Ids after the ":" in Mod*:) #array
4
  • What you need to do is first split on "|" to give you an array of strings of the form Mod1:num,num,num and then loop through that array and split each element on ":" to separate out the IDs from the names. Commented Apr 17, 2013 at 16:44
  • 1
    You should specify what kind of output you want Commented Apr 17, 2013 at 16:55
  • probably cause you didn't describe your problem in detail Commented Apr 17, 2013 at 17:06
  • you shouldn't do String = 'whatever' because that will effectively remove your String class from your Ruby interpreter by overloading it with one fixed string ;-) Commented Apr 17, 2013 at 20:58

5 Answers 5

1

I think this does what you want:

ids = string.split("|").map {|part| [part.split(":")[0], part.split(":")[1].split(/,|;/)]}
Sign up to request clarification or add additional context in comments.

Comments

1

There are a couple of ways to do this:

# This will split the string on "|" and ":" and will return:
#   %w( Mod1 id1 Mod2 id2 Mod3 id3 ... )
ids = string.split(/[|:]/)

# This will first split on "|", and for each string, split it again on ":" and returs:
#   [ %w(Mod1 id1), %w(Mod2 id2), %w(Mod3 id3), ... ]
ids = string.split("|").map { |str| str.split(":") }

Comments

1

If you want a Hash as a result for easy access via the titles, then you could do this:

 str.split('|').inject({}){|h,x| k,v = x.split(':'); h[k] = v.split(','); h}

  => {
      "Mod1"=>["10022932", "10828075", "5946410", "13321905", "5491120", "5030731"], 
      "Mod2"=>["22704455", "22991440", "22991464", "21984312", "21777721", "21777723", "21889761", "21939852", "23091478", "22339903", "23091485", "22099714", "21998260", "22364832", "21939858", "21944274", "21944226", "22800221", "22704443", "21777728", "21777719", "21678184", "21998265", "21834900", "21984331", "22704454", "21998261", "21944214", "21862610", "21836482"], 
      "Mod3"=>["10828075", "13321905", "5491120", "5946410", "5030731", "15806212", "4100566", "4787137", "2625339", "2408317", "2646868", "19612047", "2646862", "11983534", "8591489", "19612048", "10249319", "14220471", "15806209", "13330887", "15075124", "17656842", "3056657", "5086273"], 
      "Mod4"=>["10828075", "5946410", "13321905", "5030731", "5491120", "4787137", "4100566", "15806212", "2625339", "3542205", "2408317", "2646862", "2646868"], 
      "Mod5"=>["10022932;0.2512", "10828075;0.2093", "5030731;0.1465", "5946410;0.1465", "4787137;0.1465", "2625339;0.0143", "5491120;0.0143", "13321905;0.0143", "3542205;0.0143", "15806212;0.0119", "4100566;0.0119", "19612047;0.0100", "2408317;0.0100"]
     } 

Comments

0

Untested:

all_mods = {}
string.split("|").each do |fragment|
  mod_fragments = fragment.split(":")
  all_mods[mod_fragments[0]] = mod_fragments[1].split(",")
end

Comments

0

What I ended up using thanks to @tillerjs help.

data = sting.split("|")
data.each do |mod|
   module_name = mod.split(":")[0]
   recommendations = mod.split(":")[1]
end

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.