Multidimensional Array

Hi ,

Currently I am using Ruby and a beginner

I have the following multidimensional Array:

[[value1, value1_other1 ,value1_other2], [value2,
value2_other1,value2_other2], [value3, value3_other1,value3_other2]]

but i would like to get like this

[['value1', value1_other1 , value1_other2], ['value2', value2_other1,
value2_other2], ['value3', value3_other1, value3_other2]]

Can anyone please help me in doing this

···

--
Posted via http://www.ruby-forum.com/.

Hi,

what do you mean by 'value1'? Do you want to convert the value into a
string?

Just loop through the subarrays and replace the first element of each
one with its string representation (the result of to_s).

···

--
Posted via http://www.ruby-forum.com/.

Enumerable and array documentation are your friends:

your_array = [[value1, value1_other1 ,value1_other2], [value2,
value2_other1,value2_other2], [value3, value3_other1,value3_other2]]

new_array = your_array.collect{|y| [y[0].to_s,y[1..3]].flatten}

Alan

···

On Wed, Nov 28, 2012 at 2:15 PM, John M. <lists@ruby-forum.com> wrote:

Hi ,

Currently I am using Ruby and a beginner

I have the following multidimensional Array:

[[value1, value1_other1 ,value1_other2], [value2,
value2_other1,value2_other2], [value3, value3_other1,value3_other2]]

but i would like to get like this

[['value1', value1_other1 , value1_other2], ['value2', value2_other1,
value2_other2], ['value3', value3_other1, value3_other2]]

Can anyone please help me in doing this

Thanks a lot for the help , tried

your_array.collect{|y| [y[0].to_s,y[1..3]].flatten}

and did work fine

···

--
Posted via http://www.ruby-forum.com/.

Alan Forrester wrote in post #1086898:

···

On Wed, Nov 28, 2012 at 2:15 PM, John M. <lists@ruby-forum.com> wrote:

[['value1', value1_other1 , value1_other2], ['value2', value2_other1,
value2_other2], ['value3', value3_other1, value3_other2]]

Can anyone please help me in doing this

Enumerable and array documentation are your friends:

Class: Array (Ruby 1.9.3)
Module: Enumerable (Ruby 1.9.3)

your_array = [[value1, value1_other1 ,value1_other2], [value2,
value2_other1,value2_other2], [value3, value3_other1,value3_other2]]

new_array = your_array.collect{|y| [y[0].to_s,y[1..3]].flatten}

new_array = data.collect do |y|
  [
    y[0].to_s,
    *y[1..-1]
  ]
end

--
Posted via http://www.ruby-forum.com/\.