how to create multiple different arrays using a loop

Discussion in 'Ruby' started by Andy Black, Aug 20, 2006.

  1. Andy Black

    Andy Black Guest

    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/.
    Andy Black, Aug 20, 2006
    #1
    1. Advertising

  2. Andy Black

    Luke Kanies Guest

    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
    Luke Kanies, Aug 20, 2006
    #2
    1. Advertising

  3. Luke Kanies <> 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
    Robert Klemme, Aug 20, 2006
    #3
  4. Andy Black

    Luke Kanies Guest

    On Aug 20, 2006, at 10:25 AM, Robert Klemme wrote:

    > Luke Kanies <> 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
    Luke Kanies, Aug 20, 2006
    #4
  5. Andy Black

    Andy Black Guest

    Luke Kanies wrote:
    > On Aug 20, 2006, at 10:25 AM, Robert Klemme wrote:
    >
    >> Luke Kanies <> 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/.
    Andy Black, Aug 20, 2006
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -
    Replies:
    12
    Views:
    675
    Remon van Vliet
    Jun 15, 2005
  2. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
  3. a s
    Replies:
    16
    Views:
    4,555
    JustJohn
    Mar 8, 2011
  4. Kev Jackson
    Replies:
    2
    Views:
    100
  5. Isaac Won
    Replies:
    9
    Views:
    349
    Ulrich Eckhardt
    Mar 4, 2013
Loading...

Share This Page