HighDots Forums  

lookup tables

Javascript JavaScript language (comp.lang.javascript)


Discuss lookup tables in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Fabian
 
Posts: n/a

Default lookup tables - 11-08-2003 , 10:17 PM






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!
http://www.lajzar.co.uk


Reply With Quote
  #2  
Old   
Janwillem Borleffs
 
Posts: n/a

Default Re: lookup tables - 11-09-2003 , 05:28 AM







"Fabian" <lajzar (AT) hotmail (DOT) com> schreef in bericht
news:bokemu$1f2n90$2 (AT) ID-174912 (DOT) news.uni-berlin.de...
Quote:
But if I have text, is there an easy way to lookup the other columns in
this kind of table?

// 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





Reply With Quote
  #3  
Old   
Fabian
 
Posts: n/a

Default Re: lookup tables - 11-09-2003 , 06:01 AM



Janwillem Borleffs hu kiteb:

Quote:
"Fabian" <lajzar (AT) hotmail (DOT) com> schreef in bericht
news:bokemu$1f2n90$2 (AT) ID-174912 (DOT) news.uni-berlin.de...

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


// Note that I have removed the last comma in the definition of redraw
Duly noted.

Quote:
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)
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)

Quote:
return redraw[i];
Presumably, if I know the exact column I want from the table, I can use
instead:

return redraw[i][ column_number ]

Quote:
}
return false;
}

--
--
Fabian
Visit my website often and for long periods!
http://www.lajzar.co.uk



Reply With Quote
  #4  
Old   
Lasse Reichstein Nielsen
 
Posts: n/a

Default Re: lookup tables - 11-09-2003 , 06:14 AM



"Fabian" <lajzar (AT) hotmail (DOT) com> writes:

Quote:
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?
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 redrawDictionary = new Object();
for (var i=0;i<redraw.length;i++) {
redrawDictionary[redraw[i][3]] = i;
}
---
You can then make a lookup:
---
var row = redrawDictionary["south"]; // row == 4
---
/L
--
Lasse Reichstein Nielsen - lrn (AT) hotpop (DOT) com
DHTML Death Colors: <URL:http://www.infimum.dk/HTML/rasterTriangleDOM.html>
'Faith without judgement merely degrades the spirit divine.'


Reply With Quote
  #5  
Old   
Janwillem Borleffs
 
Posts: n/a

Default Re: lookup tables - 11-09-2003 , 06:16 AM




"Fabian" <lajzar (AT) hotmail (DOT) com> schreef in bericht
news:bol7e1$1g0vb3$1 (AT) ID-174912 (DOT) news.uni-berlin.de...
Quote:
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)

Yes, when the arrays have a fixed length

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

return redraw[i][ column_number ]

Shure.


JW





Reply With Quote
Reply




Thread Tools
Display Modes

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.