Nested or Multidimensional Arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TheKeith

    #1

    Nested or Multidimensional Arrays

    I can't figure out why this doesn't work:

    ---------------------------------------------
    greeting = new Array();

    greeting[0][0] = "hey";
    greeting[0][1] = "bye";


    trace(greeting[0][0] + greeting[0][1]);

    ---------------------------------------------

    Shouldn't this automatically create a multidimensiona l array? Can someone
    help me? Thanks.


  • TheKeith

    #2
    Re: Nested or Multidimensiona l Arrays

    sorry I forgot to mention that the trace part is because it's something I'm
    actually doing in actionscript, but since actionscriptis based on
    javascript, I figured I could get a good answer here too.



    "TheKeith" <no@spam.com> wrote in message
    news:9bmdnWmYVO vBhfLdRVn-uw@giganews.com ...[color=blue]
    > I can't figure out why this doesn't work:
    >
    > ---------------------------------------------
    > greeting = new Array();
    >
    > greeting[0][0] = "hey";
    > greeting[0][1] = "bye";
    >
    >
    > trace(greeting[0][0] + greeting[0][1]);
    >
    > ---------------------------------------------
    >
    > Shouldn't this automatically create a multidimensiona l array? Can someone
    > help me? Thanks.
    >
    >[/color]


    Comment

    • Michael Winter

      #3
      Re: Nested or Multidimensiona l Arrays

      On Sat, 3 Apr 2004 15:02:37 -0500, TheKeith <no@spam.com> wrote:
      [color=blue]
      > greeting = new Array();
      >
      > greeting[0][0] = "hey";
      > greeting[0][1] = "bye";
      >
      > Shouldn't this automatically create a multidimensiona l array? [...][/color]

      No, it shouldn't.

      An array is just an array, and with "= new Array()" or "= []", all of its
      elements are undefined. As such, using myArray[ index ] will return
      undefined, and attempting to subscript that is simply an error.

      You'll have to assign an array to each element of the original array to
      make a multi-dimensional array. You could either do this explicitly, or
      write two functions; one to set, and the other to get elements at the
      given indicies. They could check if the sub-array exists within the first,
      and if not, create it before assigning a value to it. Similarly, if the
      sub-array doesn't exist when trying to get a value, return undefined but
      without causing an error (by not actually accessing a non-existent array).

      Mike

      --
      Michael Winter
      M.Winter@blueyo nder.co.invalid (replace ".invalid" with ".uk" to reply)

      Comment

      • Richard A. DeVenezia

        #4
        Re: Nested or Multidimensiona l Arrays

        TheKeith wrote:
        [color=blue]
        > Shouldn't this automatically create a multidimensiona l array? Can
        > someone help me? Thanks.[/color]

        I use this helper function to create (and debug) multi-dimensional arrays.

        //====== Multidimensiona l array

        function MDArray (dimensions) {
        // instantiate objects representing a multi dimensional array
        // dimensions - array of numbers, ith number is nunmber of elements in
        dimension i

        this.length = dimensions[0]
        this.dimensions = dimensions
        if (dimensions.len gth>0) {
        for (var i=0;i<this.leng th;i++) {
        this[i] = new MDArray(dimensi ons.slice(1))
        }
        }
        }
        MDArray.prototy pe = new Array()
        MDArray.prototy pe.constructor = MDArray
        MDArray.prototy pe.toHTML = function(addr) {
        var html = ''
        if (this.dimension s.length > 2) {
        for (var i=0;i<this.dime nsions[0];i++) {
        html += this[i].toHTML((addr?a ddr+',':'')+i)
        }
        }
        else
        if (this.dimension s.length == 2) {

        html += '<BR>'+argument s[0]+'<TABLE BORDER=1>'
        for (var i=0;i<this.dime nsions[0];i++) { html += '<TR>'
        for (var j=0;j<this.dime nsions[1];j++) { html += '<TD>'
        html += this[i][j] + '</TD>'
        } html += '</TR>'
        } html += '</TABLE>'
        }
        else {
        html = this.toString()
        } return html
        }

        mda = new MDArray([4,4,4,4])
        mda[0][0][0][0] = 0
        mda[1][1][1][1] = 1
        mda[2][2][2][2] = 2
        mda[3][3][3][3] = 4
        document.write (mda.toHTML())

        --
        Richard A. DeVenezia



        Comment

        • TheKeith

          #5
          Re: Nested or Multidimensiona l Arrays


          "Michael Winter" <M.Winter@bluey onder.co.invali d> wrote in message
          news:opr5wfbpt8 5vklcq@news-text.blueyonder .co.uk...[color=blue]
          > On Sat, 3 Apr 2004 15:02:37 -0500, TheKeith <no@spam.com> wrote:
          >[color=green]
          > > greeting = new Array();
          > >
          > > greeting[0][0] = "hey";
          > > greeting[0][1] = "bye";
          > >
          > > Shouldn't this automatically create a multidimensiona l array? [...][/color]
          >
          > No, it shouldn't.
          >
          > An array is just an array, and with "= new Array()" or "= []", all of its
          > elements are undefined. As such, using myArray[ index ] will return
          > undefined, and attempting to subscript that is simply an error.
          >
          > You'll have to assign an array to each element of the original array to
          > make a multi-dimensional array. You could either do this explicitly, or
          > write two functions; one to set, and the other to get elements at the
          > given indicies. They could check if the sub-array exists within the first,
          > and if not, create it before assigning a value to it. Similarly, if the
          > sub-array doesn't exist when trying to get a value, return undefined but
          > without causing an error (by not actually accessing a non-existent array).[/color]

          thanks a lot Mike, I got it working ok now.


          Comment

          • TheKeith

            #6
            Re: Nested or Multidimensiona l Arrays


            "Richard A. DeVenezia" <radevenz@ix.ne tcom.com> wrote in message
            news:c4nrpl$2l7 kmr$1@ID-168040.news.uni-berlin.de...[color=blue]
            > TheKeith wrote:
            >[color=green]
            > > Shouldn't this automatically create a multidimensiona l array? Can
            > > someone help me? Thanks.[/color]
            >
            > I use this helper function to create (and debug) multi-dimensional arrays.
            >
            > //====== Multidimensiona l array
            >
            > function MDArray (dimensions) {
            > // instantiate objects representing a multi dimensional array
            > // dimensions - array of numbers, ith number is nunmber of elements in
            > dimension i
            >
            > this.length = dimensions[0]
            > this.dimensions = dimensions
            > if (dimensions.len gth>0) {
            > for (var i=0;i<this.leng th;i++) {
            > this[i] = new MDArray(dimensi ons.slice(1))
            > }
            > }
            > }
            > MDArray.prototy pe = new Array()
            > MDArray.prototy pe.constructor = MDArray
            > MDArray.prototy pe.toHTML = function(addr) {
            > var html = ''
            > if (this.dimension s.length > 2) {
            > for (var i=0;i<this.dime nsions[0];i++) {
            > html += this[i].toHTML((addr?a ddr+',':'')+i)
            > }
            > }
            > else
            > if (this.dimension s.length == 2) {
            >
            > html += '<BR>'+argument s[0]+'<TABLE BORDER=1>'
            > for (var i=0;i<this.dime nsions[0];i++) { html += '<TR>'
            > for (var j=0;j<this.dime nsions[1];j++) { html += '<TD>'
            > html += this[i][j] + '</TD>'
            > } html += '</TR>'
            > } html += '</TABLE>'
            > }
            > else {
            > html = this.toString()
            > } return html
            > }
            >
            > mda = new MDArray([4,4,4,4])
            > mda[0][0][0][0] = 0
            > mda[1][1][1][1] = 1
            > mda[2][2][2][2] = 2
            > mda[3][3][3][3] = 4
            > document.write (mda.toHTML())
            >
            > --
            > Richard A. DeVenezia
            > http://www.devenezia.com/downloads/sas/macros/?m=xmlib[/color]


            Thanks a lot, Richard, but it's a little more than I needed to do. I just
            realized that I needed to initiate a new instance of the nested array:

            array1[1] = new Array();

            --that works fine for me.


            Comment

            Working...