Champak Ch | 10 Jul 2012 18:16

Array in an array - how to assign to individual array?

I have 2 arrays.

a=["array1", "array2"] that stores the names of arrays as a string and

b=[["1","2"],["3","4"]] that has the values for each of these arrays.

I want to access individual arrays specifically and the contents of a
and b keep changing. So, I have to store it in a different array.
I want to create 2 new arrays array1 and array2 with values from b. Like
this.

array1= ["1","2"]
array2= ["3","4"]

Can you please suggest some ideas?

-Champak

--

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

Jesús Gabriel y Galán | 10 Jul 2012 18:23
Picon
Gravatar

Re: Array in an array - how to assign to individual array?

On Tue, Jul 10, 2012 at 6:16 PM, Champak Ch <lists <at> ruby-forum.com> wrote:
> I have 2 arrays.
>
> a=["array1", "array2"] that stores the names of arrays as a string and
>
> b=[["1","2"],["3","4"]] that has the values for each of these arrays.
>
> I want to access individual arrays specifically and the contents of a
> and b keep changing. So, I have to store it in a different array.
> I want to create 2 new arrays array1 and array2 with values from b. Like
> this.
>
> array1= ["1","2"]
> array2= ["3","4"]
>
> Can you please suggest some ideas?

I'm not really sure I understand what you want to achieve, but when
you want to "name" things, a possible solution is a hash:

arrays = {"array1" => ["1", "2"], "array2" => ["3", "4"]}

arrays["array1"] # => ["1", "2"]
arrays["array2"] # => ["3", "4"]

If you could explain with a bit more detail what is the problem you
need to solve, maybe we can come up with better solutions.

Jesus.

(Continue reading)

Champak Ch | 10 Jul 2012 18:38

Re: Array in an array - how to assign to individual array?

Thank you for the quick response. I actually have the data as a hash. 
But the hash keeps changing and I need to use the names and values 
across multiple scripts. Thats the reason I need to save it elsewhere.

myhash=[{"array1"=>"1","array2"=>"3"},{"array1"=>"2","array2"=>"4"}]

So, I fetched all names and values into 2 different arrays.

a=["array1", "array2"] that stores the names of arrays as a string and

b=[["1","2"],["3","4"]] that has the values for each of these arrays.

But having separate global arrays for array1 and array2 will enable me 
to access the data anywhere, even if the hash keeps changing.

--

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

Jan E. | 10 Jul 2012 19:09

Re: Array in an array - how to assign to individual array?

Champak Ch wrote in post #1068162:
> But having separate global arrays for array1 and array2 will enable me
> to access the data anywhere, even if the hash keeps changing.

What exactly keeps changing? What might a different version of myhash 
look like? And what are the actual data behind this all?

To me myhash looks like a table with "array1" and "array2" being the 
columns and the hashes being the rows. And now you want "snapshots" of 
the columns at a certain point of time.

If that's the case, you should actually use a table object as a model 
for the data. Internally you might store the data as a hash of the form

{
  "column_name_1" => [value_11, value_12, ...],
  "column_name_2" => [value_21, value_22, ...],
  ...
}

This would also allow you to easily access the columns.

I think having nested hashes or arrays is almost always a bad idea, 
because they're hard to understand, hard to process and unsafe. It's 
mostly better to have a specialized object, which encapsulates the data 
and enforces a certain format.

--

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

(Continue reading)

Aleksey V Zapparov | 10 Jul 2012 18:39
Picon
Gravatar

Re: Array in an array - how to assign to individual array?

On Wed, 11 Jul 2012 01:16:48 +0900
Champak Ch <lists <at> ruby-forum.com> wrote:

> I have 2 arrays.
> 
> a=["array1", "array2"] that stores the names of arrays as a string and
> 
> b=[["1","2"],["3","4"]] that has the values for each of these arrays.
> 
> I want to access individual arrays specifically and the contents of a
> and b keep changing. So, I have to store it in a different array.
> I want to create 2 new arrays array1 and array2 with values from b.
> Like this.
> 
> array1= ["1","2"]
> array2= ["3","4"]
> 
> Can you please suggest some ideas?
> 
> -Champak
> 

You can create a Hash from these two arrays:

  a = ["array1", "array2"]
  b = [["1", "2"], ["3", "4"]]

  c = Hash[ [a, b].transpose ]
  # => {"array1"=>["1", "2"],"array2"=>["3", "4"]}

(Continue reading)

Jan E. | 10 Jul 2012 18:40

Re: Array in an array - how to assign to individual array?

Hi,

If the names keep changing, too, than a Hash isn't the right data 
structure.

But I agree with Jesus that it would be helpful if we knew what you 
actually want to do. Your idea seems rather strange to me.

--

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

Champak Ch | 11 Jul 2012 18:53

Re: Array in an array - how to assign to individual array?

Yes that is correct. The data I have is in a table and I have it 
converted to a hash. I was trying to overcomplicate things.

I rehashed my data and it seems to work for my needs. Thank you.

--

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


Gmane