Graal Forums  

Go Back   Graal Forums > Development Forums > NPC Scripting
FAQ Members List Calendar Today's Posts

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old 06-10-2009, 06:55 PM
The_Kez The_Kez is offline
N-Pulse Asst. Manager
The_Kez's Avatar
Join Date: Dec 2007
Posts: 106
The_Kez is on a distinguished road
Send a message via MSN to The_Kez
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
Reply With Quote
  #2  
Old 06-10-2009, 07:02 PM
DustyPorViva DustyPorViva is offline
Will work for food. Maybe
DustyPorViva's Avatar
Join Date: Sep 2003
Location: Maryland, USA
Posts: 9,589
DustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond reputeDustyPorViva has a reputation beyond repute
Send a message via AIM to DustyPorViva Send a message via MSN to DustyPorViva
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.
Reply With Quote
  #3  
Old 06-10-2009, 07:11 PM
[email protected] sid.gottlieb@googlemail.com is offline
Banned
Join Date: Mar 2008
Posts: 861
sid.gottlieb@googlemail.com will become famous soon enough
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"
Reply With Quote
  #4  
Old 06-10-2009, 07:14 PM
The_Kez The_Kez is offline
N-Pulse Asst. Manager
The_Kez's Avatar
Join Date: Dec 2007
Posts: 106
The_Kez is on a distinguished road
Send a message via MSN to The_Kez
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. :]
Reply With Quote
  #5  
Old 06-10-2009, 07:17 PM
The_Kez The_Kez is offline
N-Pulse Asst. Manager
The_Kez's Avatar
Join Date: Dec 2007
Posts: 106
The_Kez is on a distinguished road
Send a message via MSN to The_Kez
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.
Reply With Quote
  #6  
Old 06-11-2009, 05:45 AM
fowlplay4 fowlplay4 is offline
team canada
fowlplay4's Avatar
Join Date: Jul 2004
Location: Canada
Posts: 5,200
fowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond reputefowlplay4 has a reputation beyond repute
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.
__________________
Quote:
Reply With Quote
Reply


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

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

Forum Jump


All times are GMT +2. The time now is 08:37 AM.


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