Author Topic: LIKE +  (Read 21653 times)

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
LIKE +
« on: November 01, 2018, 04:39:29 PM »
This code challenge is to reproduce the Script BASIC LIKE / JOKER and WILDCARD pattern matching functionality.

LIKE Docs

I will be validating all LIKE submissions against SB native LIKE.

Extra Points

Find a user requested number of matches or ALL based on the pattern string. SB LIKE only finds the first occurrence.

Return the JOKER count as a result of LIKE rather than TRUE (-1) / FALSE (0). I always wondered why Peter didn't use JOKER(0) to contain the total number.

Another extended feature that would be nice would be each JOKER also return the starting position of the returned string value in the search string.


« Last Edit: November 02, 2018, 12:07:21 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #1 on: November 01, 2018, 08:29:41 PM »
Quote from: AIR
If you look at match.c in the SB source, you'll see that this is not a trivial thing to implement from scratch.  There's a lot of parsing going on in that module.

That's good to know. My LIKEX and JOKERS will seem to be all that more impressive.  :-*
« Last Edit: November 02, 2018, 01:25:04 AM by John »

Offline jalih

  • Advocate
  • Posts: 111
Re: LIKE +
« Reply #2 on: November 02, 2018, 12:16:04 AM »
Quote from: AIR
If you look at match.c in the SB source, you'll see that this is not a trivial thing to implement from scratch.  There's a lot of parsing going on in that module.

That's good to know. My LIKEX and JOKERS will be all that more impressive.  :-*

I know what wildcard is but what is joker?

There is a short and simple wildcard matching algorithm in a single while loop described inside the old ddj article.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #3 on: November 02, 2018, 12:34:44 AM »
Welcome jalih!

Checkout the link in the first post for the LIKE online Script BASIC documentation.

Think of LIKE as using text patterns as brackets around the data you really want. The JOKER is represented by a * which is the data not represented in the text patterns.
« Last Edit: November 02, 2018, 01:01:03 AM by John »

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #4 on: November 02, 2018, 02:07:03 AM »
LIKEX and JOKERS Preview.

LIKEX
This is syntax compatible with SB native LIKE but allows for an option parameter to indicate the number of occurances of the passed pattern string is to be found. * means all occurances. LIKEX will return the number of occurances and undef if no matches are found of the pattern string being passed.

JOKERS
JOKER is being expanded to not only return the data represented by the * placeholder but the following information as well.

  • Length of the referenced JOKERS index returned data.
  • Starting position in the match string for JOKERS index returned string being referenced.

« Last Edit: November 02, 2018, 02:17:18 AM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #5 on: November 02, 2018, 10:54:42 AM »
Preliminary submission, does not include "Extra Point" items, and only uses "*":

Code: C++
  1. /*
  2.  *   "LIKE" keyword challenge submissionby AIR
  3.  *
  4.  *   Using C++
  5.  *
  6.  *   Implements the "*" option used in ScriptBasic's LIKE function
  7.  *
  8.  *   Only tested with the string below, YMMV
  9.  *
  10.  *   Compile with: g++ --std=c++11 like.cpp -o like
  11.  */
  12.  
  13. #include<iostream>
  14. #include<string>
  15. #include <regex>
  16. #include <vector>
  17.  
  18. using namespace std;
  19.  
  20. vector<string> JOKER;
  21.  
  22. int LIKE(string input,string expr) {
  23.     size_t pos = 0;
  24.     smatch match;
  25.  
  26.     while( ( pos = expr.find("*", pos) ) != string::npos) {
  27.         expr.replace(pos, 1, "(.+)");
  28.         pos += 1;
  29.     }
  30.  
  31.     regex term(expr);
  32.  
  33.     int i = regex_search(input, match, term, regex_constants::match_any);
  34.     if (match.size() > 0) {
  35.         for (int x=0 ; x < match.size(); x++){
  36.             JOKER.push_back(match[x]);
  37.         }
  38.     }
  39.     return i;
  40.  
  41. }
  42.  
  43. int main(int argc, char **argv) {
  44.  
  45.     string input = "You can see in the program that the vector 'JOKER' is declared as 'global'.";
  46.  
  47.     // SHOULD OUTPUT "JOKER is global"
  48.     if ( LIKE( input, "vector '*' is * as '*'." ) ) {
  49.         cout << JOKER[1] << " is " << JOKER[3] << endl;
  50.     }
  51.     return 0;
  52. }

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #6 on: November 02, 2018, 10:58:37 AM »
Thanks AIR for kicking this off with your C++ submission.

I hope to post my Sctipt BASIC first round submission sometime today.


Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #7 on: November 02, 2018, 11:38:02 AM »
LIKEX and JOKERS Preview.

LIKEX
This is syntax compatible with SB native LIKE but allows for an option parameter to indicate the number of occurances of the passed pattern string is to be found. * means all occurances. LIKEX will return the number of occurances and undef if no matches are found of the pattern string being passed.

JOKERS
JOKER is being expanded to not only return the data represented by the * placeholder but the following information as well.

  • Length of the referenced JOKERS index returned data.
  • Starting position in the match string for JOKERS index returned string being referenced.

Can you clarify this, it's not very clear what you mean...pseudo code is fine....

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #8 on: November 02, 2018, 12:16:50 PM »
The SB example I'll post should clear up any questions to the challenge goals. Folks should use the SB LIKE docs before going after extra points.

LIKEX  Enhance LIKE to do multiple occurrences of the pattern string.

JOKERS Extend JOKER to also provide its length and its position in the match string.

« Last Edit: November 02, 2018, 01:00:50 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #9 on: November 02, 2018, 02:04:49 PM »
Okay, here's another submission, this one also returns the position of the MATCH in the original string...

Code: C++
  1. /*
  2.  *   "LIKE" keyword challenge submissionby AIR
  3.  *
  4.  *   Using C++
  5.  *
  6.  *   Emulates the "*" option used in ScriptBasic's LIKE function
  7.  *
  8.  *   Only tested with the string below, YMMV
  9.  *
  10.  *   Compile with: g++ --std=c++11 like.cpp -o like
  11.  */
  12.  
  13. #include<iostream>
  14. #include<string>
  15. #include <regex>
  16. #include <vector>
  17.  
  18. using namespace std;
  19.  
  20. vector< pair<string,int> > JOKER;
  21. #define TEXT first
  22. #define POSITION second
  23.  
  24. int LIKE(string input,string expr) {
  25.     size_t pos = 0;
  26.     smatch match;
  27.  
  28.  
  29.     while( ( pos = expr.find("*", pos) ) != string::npos) {
  30.         expr.replace(pos, 1, "(.+)");
  31.         pos += 1;
  32.     }
  33.  
  34.     regex term(expr);
  35.  
  36.     int i = regex_search(input, match, term, regex_constants::match_any);
  37.     if (match.size() > 0) {
  38.         for (int x=0 ; x < match.size(); x++){
  39.             JOKER.push_back(make_pair(match[x],match.position(x)));
  40.         }
  41.     }
  42.     return i;
  43.  
  44. }
  45.  
  46. int main(int argc, char **argv) {
  47.  
  48.     string input = "You can see in the program that the vector 'JOKER' is declared as 'global'.";
  49.  
  50.     // SHOULD OUTPUT "JOKER" is global"
  51.     // and value of Joker[x] plus position in original string
  52.     if ( LIKE( input, "vector '*' is * as '*'." ) ) {
  53.         cout << JOKER[1].TEXT << " is " << JOKER[3].TEXT << "\n\n";
  54.         cout << JOKER[1].TEXT << " is at position: " << JOKER[1].POSITION << endl;
  55.         cout << JOKER[3].TEXT << " is at position: " << JOKER[3].POSITION << "\n\n";
  56.  
  57.     }
  58.     return 0;
  59. }
  60.  

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #10 on: November 02, 2018, 02:10:38 PM »
You're already in extra points territory this soon. WOW!

I think this challenge will result in a generic function we all will LIKE.

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #11 on: November 02, 2018, 04:01:13 PM »
Extra Extra points:  JOKER now CONTAINS the text, position, and length for the match.

Code: C++
  1. /*
  2.  *   "LIKE" keyword challenge submissionby AIR
  3.  *
  4.  *   Using C++
  5.  *
  6.  *   Emulates the "*" option used in ScriptBasic's LIKE function
  7.  *
  8.  *   Only tested with the string below, YMMV
  9.  *
  10.  *   Compile with: g++ --std=c++11 like.cpp -o like
  11.  */
  12.  
  13. #include<iostream>
  14. #include<string>
  15. #include <regex>
  16. #include <vector>
  17.  
  18. using namespace std;
  19.  
  20. struct JOKERINFO {
  21.     string text;
  22.     int length;
  23.     int positiion;
  24. };
  25.  
  26. vector< JOKERINFO > JOKER;
  27.  
  28. int LIKE(string input,string expr) {
  29.     size_t pos = 0;
  30.     smatch match;
  31.  
  32.     while( ( pos = expr.find("*", pos) ) != string::npos) {
  33.         expr.replace(pos, 1, "(.+)");
  34.         pos += 1;
  35.     }
  36.  
  37.     regex term(expr);
  38.  
  39.     int i = regex_search(input, match, term, regex_constants::match_any);
  40.     if (match.size() > 0) {
  41.         for (int x=0 ; x < match.size(); x++){
  42.             JOKER.push_back(JOKERINFO());
  43.             JOKER[x].text = match[x];
  44.             JOKER[x].length = match[x].length();
  45.             JOKER[x].positiion = match.position(x);
  46.         }
  47.     }
  48.     return i;
  49. }
  50.  
  51. int main(int argc, char **argv) {
  52.  
  53.     string input = "You can see in the program that the vector 'JOKER' is declared as 'global'.";
  54.  
  55.     // SHOULD OUTPUT "JOKER" is global"
  56.     // and value of Joker[x] plus position in original string and length of match
  57.     if ( LIKE( input, "vector '*' is * as '*'." ) ) {
  58.         cout << JOKER[1].text << " is " << JOKER[3].text << "\n\n";
  59.         cout << JOKER[1].text << " starts at position: " << JOKER[1].positiion << " and is " << JOKER[1].length << " Characters Long." <<endl;
  60.         cout << JOKER[3].text << " starts at position: " << JOKER[3].positiion << " and is " << JOKER[3].length << " Characters Long.\n\n";
  61.  
  62.     }
  63.     return 0;
  64. }
  65.  

Still waiting on your code, this took me a couple of hours in between work duties.... 8)

AIR.

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #12 on: November 02, 2018, 08:01:32 PM »
Looks like you have my JOKERS function working in C++.

I'm trying to address all the items in the challenge spec. before posting a SB submission.

It would be useful if compiled submissions could be offered as a shared object. (DLL, SO, ...)

IT's not a race or competition but a sharing of concepts in multiple languages. There are no extra points for finishing first.

« Last Edit: November 02, 2018, 09:00:34 PM by John »

Offline AIR

  • BASIC Developer
  • Posts: 932
  • Coder
Re: LIKE +
« Reply #13 on: November 02, 2018, 09:04:37 PM »
There are no extra points for finishing first.

Says the guy who's gonna finish second.... ;D ;D ;D ;D

Offline John

  • Forum Support / SB Dev
  • Posts: 3597
    • ScriptBasic Open Source Project
Re: LIKE +
« Reply #14 on: November 02, 2018, 09:11:32 PM »
Quote
Says the guy who's gonna finish second...

The guy who finishes last if others don't jump in.  :'(

You're not finished YET!

First to post half baked code will only get you admired.   8)

On a positive note, these challenges prove Script BASIC is finished and stable enough to submit entries without being embarrassed.
« Last Edit: November 02, 2018, 09:29:31 PM by John »