View Single Post
  #6  
Old 08-28-2006, 07:48 PM
JkWhoSaysNi JkWhoSaysNi is offline
Ruler of the graalaxy
Join Date: Feb 2005
Location: Great Britain
Posts: 488
JkWhoSaysNi is on a distinguished road
Send a message via ICQ to JkWhoSaysNi
Fixed. I stopped using the function name remove() and all is fine. Is it a reserved function name or used for something else?

Anyway, it all seems to be working fine now.

Heres my associatve array class. Feel free to use it.

PHP Code:
//Associative array by JkWhoSaysNi
//#CLIENTSIDE
function duplicates(array) {
  
//recursivly check for duplicates in an array
  
duplicate false;
  if(array.
size() != 0) {
    for (
1<= array.size()-1i++ ) {
      if (array[
0] == array[i]) {
        
duplicate true;
      }
   }
   if (
duplicate == false) {
     array.
delete(0);
     
duplicate duplicates(array);
   }
  }
  return 
duplicate;
}
/////////////////
//Constructor
/////////////////
public function make(keys,values) {

 
//required because keys is passed as a reference to duplicates, which then empties it.
 
this.tempkeys keys

 if (
keys.size() != values.size() || duplicates(this.tempkeys)) return false
  else {
    
this.cursor 0;
    
this.keys keys;
    
this.values values;
    return 
true;
  }
}
/////////////////
//Access
/////////////////
public function has(key) {
    return (
this.keys.index(key) >= 0);
}
public function 
item(key) {
  if (
has(key)) return this.values[this.keys.index(key)];
  else return 
false;
}
public function 
has_item(value) {
  return (
this.values.index(value) >= 0);
}
public function 
keys() {
  return 
this.keys;
}
public function 
values() {
  return 
this.values;
}
public function 
item_for_iteration() {
  return 
this.values[this.cursor];
}
public function 
key_for_iteration() {
  return 
this.keys[this.cursor];
}
public function 
cursor() {
  return 
this.cursor;
}
public function 
count() {
  return 
this.values.size();
}
public function 
off() {
  
//has the cursor reached the end of the array?
  
return (this.cursor this.keys.size()-1);
}

/////////////////
//Element change
/////////////////
public function put(key,value) {
  
//add or replace
  
if (has(key)) {
    
this.values[this.keys.index(key)] = value;
    return 
1;
    }
  else {
    
this.keys.add(key);
    
this.values.add(value);
    return 
2;
   }
}
public function 
remove_item(key) {
 if (
has(key)) {
   
this.values.delete(this.keys.index(key));
   
this.keys.delete(this.keys.index(key));
   return 
true;
  }
  else return 
false;
}

public function 
replace_key(oldkey,newkey) {
  if (
has(oldkey)) { 
    
this.keys[this.keys.index(oldkey)] = newkey;
    return 
true;
  }
  else return 
false;
}
public function 
clear_all() {
  
this.keys.clear();
  
this.values.clear();
  
this.cursor 0;
 }
/////////////////
//Cursor movement
/////////////////
public function forth() {
   if (!
off()) {
     
this.cursor++;
     return 
true;
    }
    else return 
false;
}
public function 
back() {
  if (
this.cursor this.keys.size()-1) { 
    
this.cursor--;
    return 
true;
    }
  else return 
false;
}
public function 
start() {
//Move cursor back to the start
  
this.cursor 0;
}
public function 
end() {
  
this.cursor this.keys.size()-1;
}
//
//invariant this.keys.size() == this.values.size();
//invariant this.cursor < this.values.size();
// would be nice but not supported. 
usage:
PHP Code:
arr = new TStaticVar(); 
arr.join("associative_array");

//create the array. first param is an array of keys, .second is values. 
//if it works will return true, if there is a problem (e.g. duplicate key) it will return false.
arr.make({"a","b","c"},{"one","two","three"});

//add another element
arr.put("d","four");

//retrieve value for key "d"
var = arr.item("d"); //var == "four"

//change value for key
arr.put("d","five"); //arr.item("d") == "five"

//remove item
arr.remove_item("d");

//iterate through the array
for(arr.start();!arr.off();arr.forth()) {
  
player.chat player.chat " " arr.item_for_iteration();

and a few other functions which you can see by looking at the class.
Reply With Quote