0

I have an array in the following form:

  [\"id\", 545, \"program_name\", \"VILLIANS MARATHON\", \"episode_name\", \"1-Season1:Ep.11\"]

I need to transform it to the form below:

  [545, \"VILLIANS MARATHON\", \"1-Season1:Ep.11\"]

The way Im doing this is as follows:

    #Convert a Active record hash to a 2D array
def activerecord_hash_to_datatable_array(activerecord_resultset)
array_of_arrays = Array.new()
array_of_rs_hashes = activerecord_resultset.to_a.map(&:serializable_hash)

array_of_rs_hashes.each do |rs| 
# {"id"=>1594, "program_name"=>nil, "episode_name"=>nil}
rs =  rs.flatten
#[\"id\", 545, \"program_name\", \"MARATHON\", \"episode_name\", \"1-Season1:Ep.11\"]"
rs_array = Array.new()
index = 1
while index < rs.length     
    puts "index = #{index}"     
    puts "\033[0;33m"+"#{rs[index]}"+"\033[0;37m"
    log_with_yellow("index[#{index}] " + "#{rs[index]}")
    rs_array << rs[index]
    index += 2
end
array_of_arrays <<  rs_array
end
array_of_arrays
end

I was wondering what the most efficient way to accomplish this is.

Clearly I need to retain only odd elements. But Id like to avoid iterating over all elements and comparing each elements index.

Is there a way to do this by skipping all the even elements ?

Thanks

3
  • 1
    this is no 2D array but a single array, looks like a JSON object stored in an array Commented Jan 15, 2014 at 16:34
  • 1
    @bjhaid - he has an array of such arrays. Commented Jan 15, 2014 at 16:35
  • @BroiSatse its a JSON object, check my answer and you would understand Commented Jan 15, 2014 at 16:43

5 Answers 5

3

You can do the following:

your_array.values_at(*your_array.each_index.select(&:odd?))
=> [545, "VILLIANS MARATHON", "1-Season1:Ep.11"]
Sign up to request clarification or add additional context in comments.

Comments

2
require 'json'
arr = JSON.parse("[\"id\", 545, \"program_name\", \"VILLIANS MARATHON\", \"episode_name\", \"1-Season1:Ep.11\"]")
new_arr = arr.select.with_index { |x,i| i.odd? }
p new_arr
# >> [545, "VILLIANS MARATHON", "1-Season1:Ep.11"]

1 Comment

I think this is the correct way to do it. Since it's a json, your given array isn't really an array due to the '\'s. So you would need to JSONify them to make a valid array, on which you can then perform select.
1

If array_of_rs_hashes is indeed an array of hashes, can't you just do:

res = array_of_rs_hashes.map(&:values)

Comments

1

Yep there is :

require 'json'
Hash[*JSON.parse(s)].values #=> [545, "VILLIANS MARATHON", "1-Season1:Ep.11"] 

where s = "[\"id\", 545, \"program_name\", \"VILLIANS MARATHON\", \"episode_name\", \"1-Season1:Ep.11\"]"

Comments

0

Try:

your_2d_array.map {|a| a.each_slice(2).map {|key, value| value}}

If you ahve active support, you can write it slightly more readible:

your_2d_array.map {|a| a.each_slice(2).map(&:second)}

1 Comment

But I dont want to put magic numbers in my code. Id like the code to be extendible. This wont work if used on arrays where length is bigger than 6 !

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.