Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Ruby > how to create multiple different arrays using a loop

Reply
Thread Tools

how to create multiple different arrays using a loop

 
 
Andy Black
Guest
Posts: n/a
 
      08-20-2006
Hi list,

I need to create a different array each time that I am going through a
loop.
I wonder how I can assign different names to the arrays based on the
iteration count.
For example first time that I go through the loop I want to create an
array named: a1, second one, a2 and so on.
Thanks a lot for your help.

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

 
Reply With Quote
 
 
 
 
Luke Kanies
Guest
Posts: n/a
 
      08-20-2006
On Aug 20, 2006, at 12:26 AM, Andy Black wrote:

> Hi list,
>
> I need to create a different array each time that I am going through a
> loop.
> I wonder how I can assign different names to the arrays based on the
> iteration count.
> For example first time that I go through the loop I want to create an
> array named: a1, second one, a2 and so on.
> Thanks a lot for your help.


By far the easiest way to do this is to stick them in a hash:

hash = {}
10.times do |count|
hash["a" + count.to_s] = [....]
end

This is, um, very similar to an array, except with "a<count>" as the
subscript instead of <count>.

If you absolutely must have a variable, then AFAIK you have to use an
instance variable:

10.times do |count|
instance_variable_set("@a#{count}", [...])
end

There might be a way to create a local variable, but even if you did
it would go out of scope as soon as the block ended, so it would be
pretty useless.

--
Luke Kanies
http://madstop.com | http://reductivelabs.com | 615-594-8199



 
Reply With Quote
 
 
 
 
Robert Klemme
Guest
Posts: n/a
 
      08-20-2006
Luke Kanies <(E-Mail Removed)> wrote:
> On Aug 20, 2006, at 12:26 AM, Andy Black wrote:
>
>> Hi list,
>>
>> I need to create a different array each time that I am going through
>> a loop.
>> I wonder how I can assign different names to the arrays based on the
>> iteration count.
>> For example first time that I go through the loop I want to create an
>> array named: a1, second one, a2 and so on.
>> Thanks a lot for your help.

>
> By far the easiest way to do this is to stick them in a hash:


I'd say Jeffrey's approach (i.e. stick those arrays into an array) is by far
the easiest and hassle free approach. Your approach has the disadvantage
that it will loose creation order because alphanumeric sorting of names like
"a1", "a2" etc. will only work until "a9".

Kind regards

robert

 
Reply With Quote
 
Luke Kanies
Guest
Posts: n/a
 
      08-20-2006
On Aug 20, 2006, at 10:25 AM, Robert Klemme wrote:

> Luke Kanies <(E-Mail Removed)> wrote:
>> By far the easiest way to do this is to stick them in a hash:

>
> I'd say Jeffrey's approach (i.e. stick those arrays into an array)
> is by far
> the easiest and hassle free approach. Your approach has the
> disadvantage
> that it will loose creation order because alphanumeric sorting of
> names like
> "a1", "a2" etc. will only work until "a9".


Sure; if you need to be able to sort based on name, use an array, I
suppose. OP implied naming was more important than ordering, which
usually means a hash, but either is sufficient, clearly.

--
Luke Kanies
http://madstop.com | http://reductivelabs.com | 615-594-8199



 
Reply With Quote
 
Andy Black
Guest
Posts: n/a
 
      08-20-2006
Luke Kanies wrote:
> On Aug 20, 2006, at 10:25 AM, Robert Klemme wrote:
>
>> Luke Kanies <(E-Mail Removed)> wrote:
>>> By far the easiest way to do this is to stick them in a hash:

>>
>> I'd say Jeffrey's approach (i.e. stick those arrays into an array)
>> is by far
>> the easiest and hassle free approach. Your approach has the
>> disadvantage
>> that it will loose creation order because alphanumeric sorting of
>> names like
>> "a1", "a2" etc. will only work until "a9".

>
> Sure; if you need to be able to sort based on name, use an array, I
> suppose. OP implied naming was more important than ordering, which
> usually means a hash, but either is sufficient, clearly.


Thanks to all of you...I used your suggestions and my code is working
fine

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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Triple nested loop python (While loop insde of for loop inside ofwhile loop) Isaac Won Python 9 03-04-2013 10:08 AM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
two arrays problem (although different from the other two arrays) Kev Jackson Ruby 2 03-29-2006 03:58 PM
Condition outside loop or separate loop for different condition? - Java 12 06-15-2005 08:50 AM



Advertisments