Conrad Lender wrote:
On 2008-11-15 17:55, The Natural Philosopher wrote:
>What I need is the correct syntax for declaring it, adding to it, and
stepping through it to extract values.
Sparse arrays are usually handled with linked lists. JS doesn't provide
built-in support for linked lists, but they be implemented quite easily.
The problem is that you want to use linked-list functionality ("stepping
through it to extract values"), but also insist on having numeric
indices on the list. Usually you'd go from one node in the list to the
next (or previous), passing a reference node as an argument. If you
don't provide a reference node, the list methods would have to search up
and down to adjust the 'next' and 'prev' properties of the neighboring
nodes. It's possible, but not very efficient if your indices are far apart.
Since you're more experienced in low-level languages, why not tell us
what you're trying to do? There may be a better way in a high-level
language like JS.
>So easy in Assembler..;-)
Um, use Assembler then?
My browser doesn't support it.
Anyway I found a fragment that was close enough for me to guess my way
through.
For interests sake,here is the code..that works.
**********************************************
<script type="text/javascript" language="JavaScript">
var mymenu =new Array() // array for our menus.
function Mentry(lev,obj)
// this was the breakthrough. Object not struct..
{
this.lev=lev;
this.obj=obj;
}
function expand (level,ourparent)
// expands a menu at this level with this parent id.
// Kills all menus at this level or below as well
{
var allmenus;
var exp=/L/;
// look for divs with Id set to LXXPYYY
//where XX is the level, and yyy is the parent id
var i;
var idstring;
level=Number(level);
ourparent=Number(ourparent);
// this registers a div layer that has been switched on..
// the reason is to make IE7 faster with a lot of
// DOM to search through. This way it only
// searches the whole DOM once. when a layer is enabled
if(!(mymenu[ourparent])) // not yet registered
{
allmenus=document.getElementsByTagName('DIV'); // get all the divs
for (i=0; i<allmenus.length;i++)
{
var sample=allmenus[i];
if (exp.test(sample.id)) // Its a LEVEL div, which we want..
{
if (Number(sample.id.slice(1,3))==level+1)
{
if (Number(sample.id.slice(4))==Number(ourparent))
{
mymenu[ourparent]=new Mentry();
// second breakhrough. Have to create element
// as cant reserve mem. Presumablye 'array' has imlicit?
mymenu[ourparent].lev=level+1;
mymenu[ourparent].obj=sample;
}
}
}
}
}
document.getElementById("glass").style.display='bl ock';
// and lay the drip tray for mouse droppings underneath..
for (i in mymenu) // skip through registered layers
{
// kill any layers at my level or below..except me.
if(mymenu[i].lev>level) //needs zapping?
{
if(i==ourparent) // that's us
mymenu[i].obj.style.display='block';
else mymenu[i].obj.style.display='none';
}
}
}
// called on mouse over the 'mouse dropping' glass layer
// (transparent overlay of everything under the flyouts)
function contract() // simply clears all except level 0 menus
{
expand(0,0); // kill all submenus
document.getElementById("glass").style.display='no ne';
}
*********************
As I said the reason was to speed up IE7 screen updates, which on an
oldish machine were abysmally slow. I reasoned that searching through
every DIV in the DOM to find the ones that I needed to flip was simply
too much like hard work for it, and the other alternative, working out
at the php stage the ID of *every* layer that needed toggling when I
moused over one of several thousand possible little divs would make a
large page even larger. The matrix of the switchable layers (actually
much smaller than the number of trigger points on the document) can be
scanned pretty fast.
In use its quite amusing as the first mouseover menu event is slow, but
as it 'learns' the map it speeds up immensely.
If I could be bothered, i'd precompute the matrix at php time, but this
is good enough, and I have more important things to do.