Array question - How dynamic is dynamic?

Discussion in 'Ruby' started by Ruby Student, Apr 7, 2009.

  1. Ruby Student

    Ruby Student Guest

    [Note: parts of this message were removed to make it a legal post.]

    Hello all you happy people!

    I have been thinking about asking this question but did not know how to
    start.
    So, if my question is ambiguous please let me know and I will try to make it
    clearer.

    Given an NxN array or even an NxM array mA from which at times I break it
    into sub arrays, I would like to be able to manipulate the sub arrays and
    have the changes dynamically propagated to the main array mA.
    For example, assume I have main array mA as listed below.
    From mA I strip say row[2], which I named sAr2 and which has values: sAr2 =
    [D, C, B, A]
    I also created sub arrays of columns, as listed below and furthermore, I
    created quadrants sub arrays.

    I would like to make a change on any of the sub arrays and have that change
    propagated simultaneously to the main array.
    For instance, say I set sAr2[1] = Y, I would like element mA[2,1] to change
    to Y from C.
    The same should happen if and when I change any of the sub arrays.
    The same should happen if and when I change the main array.

    Is this possible say in 1.9.1? or do I need some kind of GEM?


    Main Array:

    mA = [
    [A, B, C, D],
    [B, A, C, D],
    [D, C, B, A],
    [C, A, D, B]
    ]

    Sub Arrays:
    sAr0 (sub array row 0 to row n, where n is 3 for this case)
    sAr0 = [A, B, C, D]
    sAr1 = [B, A, C, D]
    sAr2 = [D, C, B, A]
    sAr3 = [C, A, D, B]

    sAc0 (sub array column 0 to column n, where n is 3 for this case)
    sAc0 = [A, B, D, C]
    sAc1 = [B, A, C, A]
    sAc2 = [C, C, B, D]
    sAc3 = [D, D, A, B]
    Four "quadrants?" arrays as follows:
    qA1:
    [[A, B
    B, A]]

    qA3:
    [[B, D
    A, B]]

    Etc.

    Thank you

    --
    Ruby Student
    Ruby Student, Apr 7, 2009
    #1
    1. Advertising

  2. On Tue, Apr 7, 2009 at 2:50 PM, Ruby Student <> wrote:
    > Hello all you happy people!
    >
    > I have been thinking about asking this question but did not know how to
    > start.
    > So, if my question is ambiguous please let me know and I will try to make it
    > clearer.
    >
    > Given an NxN array or even an NxM array mA from which at times I break it
    > into sub arrays, I would like to be able to manipulate the sub arrays and
    > have the changes dynamically propagated to the main array mA.
    > For example, assume I have main array mA as listed below.
    > From mA I strip say row[2], which I named sAr2 and which has values: sAr2 =
    > [D, C, B, A]
    > I also created sub arrays of columns, as listed below and furthermore, I
    > created quadrants sub arrays.
    >
    > I would like to make a change on any of the sub arrays and have that change
    > propagated simultaneously to the main array.
    > For instance, say I set sAr2[1] = Y, I would like element mA[2,1] to change
    > to Y from C.


    First thing that came to mind is to add a level of indirection, so
    that the array doesn't
    contain the actual values, but an object that contains the values, like so:

    irb(main):001:0> O = Struct.new :value
    => O
    irb(main):008:0> A = "A"
    => "A"
    irb(main):009:0> B = "B"
    => "B"
    irb(main):010:0> C = "C"
    => "C"
    irb(main):013:0> myAr = [[O.new(A), O.new(B), O.new(C)],[O.new(B),
    O.new(A), O.new(C)],[O.new(C), O.new(B), O.new(A)]]
    => [[#<struct O value="A">, #<struct O value="B">, #<struct O
    value="C">], [#<struct O value="B">, #<struct O value="A">, #<struct O
    value="C">], [#<struct O value="C">, #<struct O value="B">, #<struct O
    value="A">]]
    irb(main):017:0> first_row = myAr[0]
    => [#<struct O value="A">, #<struct O value="B">, #<struct O value="C">]
    irb(main):018:0> Y = "Y"
    => "Y"
    irb(main):019:0> first_row[2].value = Y
    => "Y"
    irb(main):020:0> myAr
    => [[#<struct O value="A">, #<struct O value="B">, #<struct O
    value="Y">], [#<struct O value="B">, #<struct O value="A">, #<struct O
    value="C">], [#<struct O value="C">, #<struct O value="B">, #<struct O
    value="A">]]

    So by changing the value of the object pointed to by first_row[0], you
    are really changing the same object that is referenced in the original
    array. Don't know if this is convenient for you or not. Probably
    someone will come up with a better idea.

    Hope this helps,

    Jesus.
    Jesús Gabriel y Galán, Apr 7, 2009
    #2
    1. Advertising

  3. Ruby Student

    Mark Thomas Guest

    On Apr 7, 8:50 am, Ruby Student <> wrote:
    > Main Array:
    >
    > mA = [
    > [A, B, C, D],
    > [B, A, C, D],
    > [D, C, B, A],
    > [C, A, D, B]
    > ]
    >
    > Sub Arrays:
    > sAr0 (sub array row 0 to row n, where n is 3 for this case)
    > sAr0 = [A, B, C, D]
    > sAr1 = [B, A, C, D]
    > sAr2 = [D, C, B, A]
    > sAr3 = [C, A, D, B]


    I suggest you use the Matrix class (in the standard library).

    Though for the row case, you don't need anything special:
    sAr0 = mA[0] = [A, B, C, D]
    sAr1 = mA[1] = [B, A, C, D]
    sAr2 = mA[2] = [D, C, B, A]
    sAr3 = mA[3] = [C, A, D, B]
    If you change sAr0, it will be reflected in mA[0].

    -- Mark.
    Mark Thomas, Apr 7, 2009
    #3
  4. On 07.04.2009 15:38, Mark Thomas wrote:
    > On Apr 7, 8:50 am, Ruby Student <> wrote:
    >> Main Array:
    >>
    >> mA = [
    >> [A, B, C, D],
    >> [B, A, C, D],
    >> [D, C, B, A],
    >> [C, A, D, B]
    >> ]
    >>
    >> Sub Arrays:
    >> sAr0 (sub array row 0 to row n, where n is 3 for this case)
    >> sAr0 = [A, B, C, D]
    >> sAr1 = [B, A, C, D]
    >> sAr2 = [D, C, B, A]
    >> sAr3 = [C, A, D, B]

    >
    > I suggest you use the Matrix class (in the standard library).


    Are you sure this works? It seems, when I create a row Vector of the
    Matrix it is read only. At least []= is defined private.

    But I agree to the general advice to use a special class for this and
    not try to work with nested Arrays. That's what OO is for. Defining a
    two dimensional matrix with Vector proxies isn't too hard.

    Kind regards

    robert
    Robert Klemme, Apr 7, 2009
    #4
  5. Ruby Student

    Ruby Student Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Tue, Apr 7, 2009 at 11:59 AM, Robert Klemme
    <>wrote:

    > On 07.04.2009 15:38, Mark Thomas wrote:
    >
    >> On Apr 7, 8:50 am, Ruby Student <> wrote:
    >>
    >>> Main Array:
    >>>
    >>> mA = [
    >>> [A, B, C, D],
    >>> [B, A, C, D],
    >>> [D, C, B, A],
    >>> [C, A, D, B]
    >>> ]
    >>>
    >>> Sub Arrays:
    >>> sAr0 (sub array row 0 to row n, where n is 3 for this case)
    >>> sAr0 = [A, B, C, D]
    >>> sAr1 = [B, A, C, D]
    >>> sAr2 = [D, C, B, A]
    >>> sAr3 = [C, A, D, B]
    >>>

    >>
    >> I suggest you use the Matrix class (in the standard library).
    >>

    >
    > Are you sure this works? It seems, when I create a row Vector of the
    > Matrix it is read only. At least []= is defined private.
    >
    > But I agree to the general advice to use a special class for this and not
    > try to work with nested Arrays. That's what OO is for. Defining a two
    > dimensional matrix with Vector proxies isn't too hard.
    >
    > Kind regards
    >
    > robert
    >
    >

    Thank you all for your input and suggestions!

    --
    Ruby Student
    Ruby Student, Apr 9, 2009
    #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. Peter B. Steiger

    Can a static array contain a dynamic array of pointers?

    Peter B. Steiger, Apr 19, 2004, in forum: C Programming
    Replies:
    8
    Views:
    2,073
    Dave Thompson
    Apr 26, 2004
  2. Replies:
    2
    Views:
    1,534
    Jack Klein
    Jul 14, 2005
  3. Mara Guida

    const and array of array (of array ...)

    Mara Guida, Sep 2, 2009, in forum: C Programming
    Replies:
    3
    Views:
    463
    David RF
    Sep 3, 2009
  4. Tom
    Replies:
    3
    Views:
    186
    salsablr
    Dec 20, 2004
  5. Tuan  Bui
    Replies:
    14
    Views:
    451
    it_says_BALLS_on_your forehead
    Jul 29, 2005
Loading...

Share This Page