Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Wildcard Finder (https://forums.graalonline.com/forums/showthread.php?t=84614)

Chompy 03-10-2009 11:53 PM

Wildcard Finder
 
I got bored, so I made this simple wildcard finder.

PHP Code:

// Made by Chompy
/* 
   Only supports one wildcard!

   find(str, arr[], bool)

   Syntax defines what to search for.

   Array is a member of all strings to compare with.

   Strict determines if it will use stricter filter rules on output.
   ("foo*" would not return "foo" in the output array for example)

*/
function find(syntax, array, strict) {
  
temp.pos = syntax.pos("*");
  if (
pos == -1) {
    return {array[array.
index(syntax)]};
  }
  else {
    
temp.a = 0; temp.b = ""; temp.c = "";
    if (
pos < 1) { // *x
      
a = 1;
      
c = syntax.substring(1);
    }
    if (!(
pos == syntax.length()-1) && a == 0) { // x*x
      
a = 2;
      
c = syntax.substring(pos+1);
      
b = syntax.substring(0, syntax.length()-1-c.length());
    }
    if (
pos == syntax.length()-1) { // x*
      
a = 3;
      
b = syntax.substring(0, pos);
    }
    if (
a > 0) {
      
temp.out = 0;
      
temp.compare = syntax.substring(0, pos);
      
compare @= syntax.substring(pos+1);
      for(
temp.i : array) {
        if (
strict && i == compare) continue;
        if (
a == 1) {
          if (
i.ends(c)) out.add(i);
        }
        else if (
a == 2) {
          if (
i.starts(b) && i.ends(c)) out.add(i);
        }
        else if (
a == 3) {
          if (
i.starts(b)) out.add(i);
        }
      }
      return 
out;
    }
  }
  return 
0;
} 

Example:

PHP Code:

function onCreated() { 
  
temp.a = {"foo", "foo1", "bar", "barfoo", "baroo"};
  
  
temp.b = find("foo*", a); // foo,foo1
  
temp.c = find("foo*", a, true); // foo1,
  
  
temp.d = find("bar*oo", a); // barfoo,baroo
  
temp.e = find("bar*oo", a, true); // barfoo
  
  
temp.f = find("*o", a); // foo,barfoo,baroo
  
  
temp.g = find("*", a); // foo,foo1,bar,barfoo,baroo
} 

Please post any bugs and criticism below I guess.

cbk1994 03-11-2009 12:08 AM

Very nice, I like it.

Codein 03-11-2009 12:39 AM

Nice work man :D

Chompy 03-11-2009 07:52 AM

Thanks guys.

I'll probably release a new version soon though, which will support more then one wildcard :]

MiniOne 03-11-2009 08:10 AM

Seems right :)
Good work keep it up.

Dan 03-12-2009 12:34 AM

This was a bit rushed but here is my version of the Wildcard Finder ^^ I hope it works as you want it to work.

PHP Code:

function onCreated() {
  
temp.a = {"foo1231594","foo437793","bar1234","bar4321","moo4567"};
  
temp.r = findWildcard("foo*3*9*",temp.a); // foo1231594,foo437793
} 

PHP Code:

function findWildcard(syntax,array) {
  if (
temp.syntax == "*")
    return 
temp.array;
  if (
temp.syntax.pos("*") == - 1)
    return (
temp.array.index(temp.syntax) > - 1 ? temp.array[temp.array.index(temp.syntax)] : false);
  
  
temp.newsyntax = {};  
  
this.syntaxend = !temp.syntax.ends("*");  
  while (
temp.syntax.pos("*") > - 1) {
    if (!(
temp.syntax.pos("*") in {0,temp.syntax.length()}))
      
temp.newsyntax.add(temp.syntax.substring(0,temp.syntax.pos("*")));
    
temp.syntax = temp.syntax.substring(temp.syntax.pos("*") + 1);
  }
  if (
temp.syntax != "")
    
temp.newsyntax.add(temp.syntax);
    
  
temp.copysyntax = {};
  for (
temp.s: temp.newsyntax) {
    
temp.r = "";
    for (
temp.l = 0; temp.l < temp.s.length(); temp.l ++)
      
temp.r @= "*";
    
temp.copysyntax.add(temp.r);
  }
  
  
this.syntax = temp.newsyntax;
  
this.syntaxcopy = temp.copysyntax;
  
  
temp.result = {};
  for (
temp.checktext: temp.array) {
    
temp.check = checkText(temp.checktext);
    if (
temp.check != false)
      
temp.result.add(temp.check);
  }
  return (
temp.result.size() > 0 ? temp.result : false);
}

function 
checkText(realtext) {
  if (
this.syntaxend && !temp.realtext.ends(this.syntax[this.syntax.size() - 1]))
    return 
false;
  
temp.highest = - 1;
  
temp.checktext = temp.realtext;
  for (
temp.c = 0; temp.c < this.syntax.size(); temp.c ++) {
    
temp.checkpart = this.syntax[temp.c];
    if (
temp.checktext.pos(temp.checkpart) == - 1)
      return 
false;
    
    if (
temp.checktext.pos(temp.checkpart) > temp.highest) {
      
temp.checkpos = temp.checktext.pos(temp.checkpart);
      
temp.checktext = temp.checktext.substring(0,temp.checkpos) @ 
                       
this.syntaxcopy[temp.c] @ 
                       
temp.checktext.substring(temp.checkpos + this.syntaxcopy[temp.c].length());
                       
      
temp.highest = temp.checkpos;
    }
    else
      return 
false;
  }    
  return 
temp.realtext;
} 


Chompy 03-12-2009 12:53 AM

Nice script Dan ^^

Tigairius 03-12-2009 01:00 AM

Nice work guys :)

MiniOne 03-12-2009 07:53 AM

Nice Dan :)
OMGG That gives me a great idea for a zone event, I'd like to discuss it with you :D
reply my emails...

Dan 03-12-2009 07:08 PM

Thanks for the comments :) My script doesn't have the "strict" stuff yet but that shouldn't be hard.


All times are GMT +2. The time now is 07:15 PM.

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