playMaker

Author Topic: Search arrays?  (Read 6084 times)

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Search arrays?
« on: May 02, 2011, 05:50:20 PM »
Just wondering if this is possible with Playmaker:

Have an array of strings, and filter them out using regular expressions.

http://msdn.microsoft.com/en-us/library/system.text.regularexpressions.regex.aspx

For instance, if you had this list:

apple
apartment
dog
market
snap
and you typed in "ap", using the regular expression:


.*ap.*
it would find the following matches:

apple
apartment
snap

The RegEx class can be found in the System.Text.RegularExpressions namespace.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Search arrays?
« Reply #1 on: May 03, 2011, 01:25:20 AM »
Hi,

 I am positive this can be wrapped into a nice little action. Will not have time this week unfortunatly, but if you don't get a working solution before then, get back to me again, and I'll have a go at it :)

 Bye,

 Jean

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Re: Search arrays?
« Reply #2 on: May 03, 2011, 05:50:26 AM »
Thanks Jean, have spent this evening trying to wrap up something myself but just end up either no result or an error. Will keep trying, but if you can have a go you might save me hours of pain!

MaDDoX

  • 1.2 Beta
  • Full Member
  • *
  • Posts: 165
    • FluidPlay Studios
Re: Search arrays?
« Reply #3 on: May 03, 2011, 10:36:02 AM »
A quick search for the Mono source got me this C# source, looks very easy to convert into a PlayMaker action. Just add the fields, replace string with fsmString, etc. Probably worth the exercise, if it works please post it in the Actions section:

Code: [Select]
//
// assembly: System
// namespace: System.Text.RegularExpressions
// file: quicksearch.cs
//
// Authors: Dan Lewis (dlewis@gmx.co.uk)
// Juraj Skripsky (juraj@hotfeet.ch)
//
// (c) 2002 Dan Lewis
// (c) 2003 Juraj Skripsky
//

//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//

using System;
using System.Collections;

namespace System.Text.RegularExpressions {
internal class QuickSearch {
// simplified boyer-moore for fast substring matching
// (for short strings, we use simple scans)
public QuickSearch (string str, bool ignore)
: this(str, ignore, false)
{
}

public QuickSearch (string str, bool ignore, bool reverse) {
this.str = str;
this.len = str.Length;
this.ignore = ignore;
this.reverse = reverse;

if (ignore)
str = str.ToLower ();

// create the shift table only for "long" search strings
if(len > THRESHOLD)
SetupShiftTable ();
}

public string String {
get { return str; }
}

public int Length {
get { return len; }
}

public bool IgnoreCase {
get { return ignore; }
}

public int Search (string text, int start, int end) {
int ptr = start;


if ( reverse )
{
if (start < end)
return -1;

if ( ptr > text.Length)
{
ptr = text.Length;
}

// use simple scan for a single-character search string
if (len == 1)
{
while (--ptr >= end)
{
if(str[0] == GetChar(text[ptr]))
return ptr ;

}
return -1;
}


if ( end < len)
end = len - 1 ;

ptr--;
while (ptr >= end)
{
int i = len -1 ;
while (str[i] == GetChar(text[ptr - len +1 + i]))
{
if (-- i < 0)
return ptr - len + 1;
}

if (ptr > end)
{
ptr -= GetShiftDistance (text[ptr - len ]);

}
else
break;
}

}
else
{
// use simple scan for a single-character search string
if (len == 1)
{
while (ptr <= end)
{
if(str[0] == GetChar(text[ptr]))
return ptr;
else
ptr++;
}
return -1;
}

if (end > text.Length - len)
end = text.Length - len;

while (ptr <= end)
{
int i = len - 1;
while (str[i] == GetChar(text[ptr + i]))
{
if (-- i < 0)
return ptr;
}

if (ptr < end)
ptr += GetShiftDistance (text[ptr + len]);
else
break;
}
}

return -1;

}

// private

private void SetupShiftTable () {
shift = new Hashtable ();
if (reverse)
{
for (int i = len ; i > 0; -- i)
{
char c = str[i -1];
shift[GetChar(c)] = i;
}
}
else
{
for (int i = 0; i < len; ++ i)
{
char c = str[i];
shift[GetChar(c)] = len - i;
}
}

}

private int GetShiftDistance (char c) {
if(shift == null)
return 1;

object s = shift [GetChar (c)];
return (s != null ? (int)s : len + 1);
}

private char GetChar(char c) {
return (!ignore ? c : Char.ToLower(c));
}

private string str;
private int len;
private bool ignore;
private bool reverse;

private Hashtable shift;
private readonly static int THRESHOLD = 5;
}

}
--
Breno "MaDDoX" Azevedo
@brenoazevedo

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Re: Search arrays?
« Reply #4 on: May 07, 2011, 04:14:21 AM »
Brilliant, Thanks for this!
Unfortunately I'm struggling to convert this.
C# is proving trickier than unity script. Can any one help out?

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Re: Search arrays?
« Reply #5 on: May 13, 2011, 12:33:21 AM »
--Bump--
Can anyone oblige?
I have tried to get this script working and while it looks okay in MonoDevelop I get nothing but errors in Unity and Playmaker.
HELP!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Search arrays?
« Reply #6 on: May 13, 2011, 01:24:53 AM »
Hi,

 I think you have to run it as a c# script, is it so or have tried to convert it and run it as a javascript?

 Bye,

 Jean

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Re: Search arrays?
« Reply #7 on: May 13, 2011, 01:41:31 AM »
Have tried it as C#.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Search arrays?
« Reply #8 on: May 13, 2011, 01:50:49 AM »
Hi,

 Can you share the script, and I'll have a look ? cheers.

 Bye,

 Jean

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Re: Search arrays?
« Reply #9 on: May 13, 2011, 01:54:16 AM »
I'm not at work, and it's Friday, but will try and login from home later. Thanks!

jasonlee

  • Playmaker Newbie
  • *
  • Posts: 12
Re: Search arrays?
« Reply #10 on: May 13, 2011, 04:29:31 AM »
Here's the action! :)

Don't have Playmaker installed on my home pc, tested blindly, let me know if any problem.
« Last Edit: May 13, 2011, 05:23:04 AM by jasonlee »

Duffdaddy

  • Junior Playmaker
  • **
  • Posts: 54
Re: Search arrays?
« Reply #11 on: May 13, 2011, 06:00:59 AM »
Jason, blimmin brilliant! Will give a whirl.