Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   NPC Scripting (https://forums.graalonline.com/forums/forumdisplay.php?f=8)
-   -   Efficient way to match unordered arrays? (https://forums.graalonline.com/forums/showthread.php?t=86270)

The_Kez 06-10-2009 06:55 PM

Efficient way to match unordered arrays?
 
I have a function which I am entering an array, and the job of the function is to match the entered array to an array within the list. It looks a little something like this...

PHP Code:

function Testarry ) {
  
temp.list = {
  {
"Test One""Test Two"},"String One",
  {
"Test Three""Test One"},"String Two",
  {
"Test Three""Test Two"},"String Three"
  
};
  
//matching script should go here
  
return appropriate string;


Thing is I can't compare them directly. Because the fed array may be in a different order. For example, if 'arry' is {"Test Two", "Test One"} I still need it two return "String One".
If there are no matches, OR if 'arry' contains a match but has extra items which are not in the list (i.e {"Test One", "Test Two", "Test Three"}) it should return false.

I could probably do some magic and come up with a way to match these, but I'd like to see some of your ideas to match them as there is probably a much better way then what I would do.

Thanks :)

DustyPorViva 06-10-2009 07:02 PM

PHP Code:

function comparearrays(arr1,arr2) {
  
temp.checklist arr2.size();
  for (
temp.arr1) {
    if (
i in arr2) {
      
checklist--;
      
arr2.remove(i);
    } else break;
  }
  return (
checklist == 0);


? I dunno, I didn't test it... seems to make sense in my head, however.

[email protected] 06-10-2009 07:11 PM

HTML Code:

function onCreateds() {
  temp.arr = {"test", "foo", "test"};
 
  temp.list = {
    {"test", "test", "foo"}, "string a",
    {"test", "test", "foos"}, "string b",
    {"test", "testa", "foo"}, "string c",
    {"foo", "test", "test"}, "string d",
    {"test", "teste"}, "string e"
  };
 
    //Let's first match the sizes
  temp.size = temp.arr.size();
 
  for (temp.i: temp.list) {
      //If it's a totally different size, ignore it
    if (temp.size != temp.i.size()) continue;

      //Now let's clear it the counter
    temp.count = 0;

    for (temp.e: temp.i) {
        //If it's in the list, let's increase the counter
      if (temp.e in temp.arr) temp.count++;
    }

      //If it's the same amount let's add to the list
    if (temp.count == temp.size) {
      //Now, let's find the index and increase it
      temp.found.add(temp.list[temp.list.index(@ temp.i) + 1]);
    }
  }
 
  return temp.found;
}

This returns "string a","string d"

The_Kez 06-10-2009 07:14 PM

Stupid of me to post this and then come up with a solution for myself 10 minutes later.
Modifying your script to how for what I wanted worked perfectly. So thanks for the help. I also came up with this script awhile after I made the thread...

PHP Code:

function Comparecombo ) {
  
temp.list = {
  {
"Test One","Test Two","Test Three"},"Test String Zero",
  {
"Test One","Test Two"},"Test String One",
  {
"Test Two","Test Three"},"Test String Two"
  
};
  for ( 
temp.: list ) {
    if ( 
i.type() == ) {
      
temp.match 0;
      for ( 
temp.combo ) {
        if ( 
j in i match++;
      }
      if ( 
match == i.size() || combo == i.size() ) return list[list.index(i)+1];
    }
  }


This also works, I've tested it a few times. :]

The_Kez 06-10-2009 07:17 PM

I think either of your two ways would be better to use since they're skipping cycles which are unnecessary instead of running through them and comparing the sizes afterwards.

fowlplay4 06-11-2009 05:45 AM

An array is pretty much just a string with comma's in it, so as long as you aren't doing crazy stuff this should get you by.

PHP Code:

function onCreated() {
  echo(
onTestFunc({"asdf""jkl;"})); // output = rawr
  
echo(onTestFunc({"rawr""asdf"})); // output = !
}
function 
onTestFunc(arr) {
  
temp.stuff = {
    {
"asdf""jklm"}, 123,
    {
"asdf""jkl."}, 124,
    {
"asdf""jkl;"}, "rawr"
  
};
  
arr arr.substring(0);
  for (
temp.0temp.stuff.size(); temp.+= 2) {
    if (
stuff[i].substring(0) == arr) return stuff[i+1];
  }
  return 
"!";


If the stuff array was properly sorted you could probably write a binary search instead of that simple for loop.


All times are GMT +2. The time now is 10:17 AM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2026, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.