lookup tables

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

    lookup tables

    var redraw = [
    [0, 0, 1, "north"],
    [1, 1, 1, "north east"],
    [2, 1, 0, "east"],
    [3, 1,-1, "south east"],
    [4, 0,-1, "south"],
    [5,-1,-1, "south west"],
    [6,-1, 0, "west"],
    [7,-1, 1, "north west"],
    ];

    With teh following table, it is easy to convert from a number row
    reference to text

    d = redraw[row][3]

    But if I have text, is there an easy way to lookup the other columns in
    this kind of table?


    --
    --
    Fabian
    Visit my website often and for long periods!


  • Janwillem Borleffs

    #2
    Re: lookup tables


    "Fabian" <lajzar@hotmail .com> schreef in bericht
    news:bokemu$1f2 n90$2@ID-174912.news.uni-berlin.de...[color=blue]
    >
    > But if I have text, is there an easy way to lookup the other columns in
    > this kind of table?
    >[/color]

    // Note that I have removed the last comma in the definition of redraw
    var redraw = [
    [0, 0, 1, "north"],
    [1, 1, 1, "north east"],
    [2, 1, 0, "east"],
    [3, 1,-1, "south east"],
    [4, 0,-1, "south"],
    [5,-1,-1, "south west"],
    [6,-1, 0, "west"],
    [7,-1, 1, "north west"]
    ];

    function findRow(text) {
    for (var i = 0; i < redraw.length; i++) {
    if (redraw[i][redraw[i].length-1] == text)
    return redraw[i];
    }
    return false;
    }

    var row = findRow("north east");
    if (row) {
    alert(row.join( ', '));
    } else {
    alert('not found');
    }


    JW



    Comment

    • Lasse Reichstein Nielsen

      #3
      Re: lookup tables

      "Fabian" <lajzar@hotmail .com> writes:
      [color=blue]
      > With teh following table, it is easy to convert from a number row
      > reference to text
      >
      > d = redraw[row][3]
      >
      > But if I have text, is there an easy way to lookup the other columns in
      > this kind of table?[/color]

      No. You have to run through the array to find the match.

      You can build a new dictionary object to make it faster. Then you only
      have to run through the array once.

      ---
      var redrawDictionar y = new Object();
      for (var i=0;i<redraw.le ngth;i++) {
      redrawDictionar y[redraw[i][3]] = i;
      }
      ---
      You can then make a lookup:
      ---
      var row = redrawDictionar y["south"]; // row == 4
      ---
      /L
      --
      Lasse Reichstein Nielsen - lrn@hotpop.com
      DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleD OM.html>
      'Faith without judgement merely degrades the spirit divine.'

      Comment

      • Fabian

        #4
        Re: lookup tables

        Janwillem Borleffs hu kiteb:
        [color=blue]
        > "Fabian" <lajzar@hotmail .com> schreef in bericht
        > news:bokemu$1f2 n90$2@ID-174912.news.uni-berlin.de...[color=green]
        >>
        >> But if I have text, is there an easy way to lookup the other columns
        >> in this kind of table?
        >>[/color]
        >
        > // Note that I have removed the last comma in the definition of redraw[/color]

        Duly noted.
        [color=blue]
        > var redraw = [
        > [0, 0, 1, "north"],
        > [1, 1, 1, "north east"],
        > [2, 1, 0, "east"],
        > [3, 1,-1, "south east"],
        > [4, 0,-1, "south"],
        > [5,-1,-1, "south west"],
        > [6,-1, 0, "west"],
        > [7,-1, 1, "north west"]
        > ];
        >
        > function findRow(text) {
        > for (var i = 0; i < redraw.length; i++) {
        > if (redraw[i][redraw[i].length-1] == text)[/color]

        In the specific case of this table, can I replace that line above with
        the following and have the same effect:

        if (redraw[i][3] == text)
        [color=blue]
        > return redraw[i];[/color]

        Presumably, if I know the exact column I want from the table, I can use
        instead:

        return redraw[i][ column_number ]
        [color=blue]
        > }
        > return false;
        > }[/color]


        --
        --
        Fabian
        Visit my website often and for long periods!


        Comment

        • Janwillem Borleffs

          #5
          Re: lookup tables


          "Fabian" <lajzar@hotmail .com> schreef in bericht
          news:bol7e1$1g0 vb3$1@ID-174912.news.uni-berlin.de...[color=blue]
          >
          > In the specific case of this table, can I replace that line above with
          > the following and have the same effect:
          >
          > if (redraw[i][3] == text)
          >[/color]

          Yes, when the arrays have a fixed length
          [color=blue]
          >
          > Presumably, if I know the exact column I want from the table, I can use
          > instead:
          >
          > return redraw[i][ column_number ]
          >[/color]

          Shure.


          JW



          Comment

          Working...