Verifiaction if string is in a data set

Hints, tips and tricks for newbies

Moderators: Dorian (MJT support), JRL

Post Reply
fylthymcnasty
Newbie
Posts: 6
Joined: Wed May 12, 2010 1:17 pm

Verifiaction if string is in a data set

Post by fylthymcnasty » Fri Jan 20, 2012 2:36 am

Hi,

I don't normally like to bug people with noob questions on forums, but I've done a lot of searching online and in the help file and I can't find what I'm looking for. It's probably here, but I have might have the terminology wrong.

I have a list of around 4000 short text strings, currently just in a text file. I want to copy a string from a webpage, verify if it's in my list. If it is, I move on to the next check, if it's not, I apply an action. I can write most of this myself, but I don't know how to create the data set and make the verification.

Ideally, for speed, I would rather not reset the focus to the text file and back again. Can I create an array, a data set, or similar within the macro scheduler script? Is this a good idea? Are there any database queries I could perform and how easy would this be?

Look, I'm not a very experienced programmer, but I've been using MS a few years now for simple stuff, and I'm quite handy with DB stuff too, but I'd rather not be switching between apps to make this check.

Any help, or advice is very welcome! Thanks :)

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Jan 20, 2012 4:33 am

Hi fylthymcnasty,

I was going to post a regex solution but this is even simpler.

It writes its own text file of strings to use as a demo but you can just point it at your 4000 string existing text file... enjoy!

Code: Select all

/*
TEXT:
Pink Hearts
Yellow Moons
Orange Stars
Green Clovers
Blue Diamonds
Purple Horseshoes
Red Balloons
Pots of Gold
Rainbows
Hourglasses
*/

Let>filename=C:\my_string_list_one_per_line.txt

LabelToVar>TEXT,values_for_text_file
WriteLn>filename,result,values_for_text_file

//load the entire text file into variable string_list 
ReadFile>filename,string_list

//switch all to UPPERCASE in our variable
UpperCase>string_list,string_list

//test 1 - no match
Let>string_scraped_from_a_website=BLUE berries
GoSub>test_the_string_and_display_result

//test 2 - match
Let>string_scraped_from_a_website=green CLOVERS
GoSub>test_the_string_and_display_result

SRT>test_the_string_and_display_result
  //switch all to UPPERCASE in our test string
  UpperCase>string_scraped_from_a_website,string_scraped_from_a_website
  Position>string_scraped_from_a_website,string_list,1,StartPos
  If>StartPos=0
    MDL>Sorry %string_scraped_from_a_website% not found in me list
  Else
    MDL>%string_scraped_from_a_website% is in me list... oh oh... they're after me Lucky Charms!
  EndIf
End>test_the_string_and_display_result
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

fylthymcnasty
Newbie
Posts: 6
Joined: Wed May 12, 2010 1:17 pm

Post by fylthymcnasty » Fri Jan 20, 2012 5:50 am

Wow man, thanks!

There's a few things there I didn't know anything about, but now you've shown me them I should be able to incorporate them into a few other things I might want to do. I just wish I had time to learn more about Macro Scheduler, it's been such a great tool for me but I know I've barely scraped the surface of what I can do with it!

I'll have a go it making this happen later tonight, and I'll let you know how I go, but on paper it looks great :)

Thanks again!

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Fri Jan 20, 2012 10:09 am

You're very welcome, glad to help and thanks for the points. Yes, let us know how it works out for you... and don't be shy about asking questions or sharing your discoveries. Reading these forums is a great way to learn... and there's quite a few regulars here that will help if you get stuck. I try to read new msgs daily to keep up with things as its too hard to catch up otherwise.

Welcome aboard and take care
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

fylthymcnasty
Newbie
Posts: 6
Joined: Wed May 12, 2010 1:17 pm

Post by fylthymcnasty » Sat Jan 28, 2012 1:32 pm

Oops, probably left this a bit late, but wanted to say thanks again, everything worked out really well and I extended myself by tightening-up my code up even further and now it runs like a dream.

Soon all the lucky charms will be mine!

Just jokes. It was all honorable stuff :)

User avatar
jpuziano
Automation Wizard
Posts: 1085
Joined: Sat Oct 30, 2004 12:00 am

Post by jpuziano » Sat Jan 28, 2012 11:46 pm

Hi fylthymcnasty,

I'm glad the code worked well for you. A few hours after I posted that, I thought of a case where that code might fail... or should I say, perhaps not perform as you intended... and your above post just reminded me so here we go...

The above code will match on parial strings... for example, if you scrape "gold" from a website, that's going to be seen as a match... even though what you have in the list is actually "pots of gold". If that is fine and exactly what you want, don't change anything, you're there.

However, if you need or would like to recognize partial matches as being partial... then the code below will do the trick.

I was going to do this without using the RegEx> command... but RegEx> makes this so easy that I can't imagine not using it in this case... however if someone wants to post the solution without using RegEx> then feel free.

In the Message Box lines, I added double quotes " on either side of the displayed variables string_scraped_from_a_website and whole_line_from_string_list to make things more clear for the user.

The code below detects non-matches, perfect matches and partial matches as well... keep an eye on those Lucky Charms!

Code: Select all

/*
TEXT:
Pink Hearts
Yellow Moons
Orange Stars
Green Clovers
Blue Diamonds
Purple Horseshoes
Red Balloons
Pots of Gold
Rainbows
Hourglasses
*/

Let>filename=C:\my_string_list_one_per_line.txt

LabelToVar>TEXT,values_for_text_file
WriteLn>filename,result,values_for_text_file

//load the entire text file into variable string_list
ReadFile>filename,string_list

//switch all to UPPERCASE in our variable
UpperCase>string_list,string_list

//test 1 - no match
Let>string_scraped_from_a_website=BLUE berries
GoSub>test_the_string_and_display_result

//test 2 - match
Let>string_scraped_from_a_website=green CLOVERS
GoSub>test_the_string_and_display_result

//test 3 - "partial" match
Let>string_scraped_from_a_website=gold
GoSub>test_the_string_and_display_result

SRT>test_the_string_and_display_result
//switch all to UPPERCASE in our test string
UpperCase>string_scraped_from_a_website,string_scraped_from_a_website
Position>string_scraped_from_a_website,string_list,1,StartPos

If>StartPos=0
  MDL>Sorry "%string_scraped_from_a_website%" not found in me list
Else
  //check if string_scraped_from_a_website matches ENTIRE line in string_list
  //its good practice to clear first var in match array prior to every use/re-use
  Let>matches_1=
  //in regex pattern below, (?m) turns on muli-line mode so we can match to individual "lines" within the var
  //[^\x0D\x0A]* will match any non CR or LF chars on either side of our search string
  Let>pattern=(?m)^[^\x0D\x0A]*%string_scraped_from_a_website%[^\x0D\x0A]*$
  RegEx>pattern,string_list,0,matches,num,0
  Let>whole_line_from_string_list=matches_1

  If>string_scraped_from_a_website=whole_line_from_string_list
    MDL>"%string_scraped_from_a_website%" is in me list... oh oh... they're after me Lucky Charms!
  Else
    MDL>"%string_scraped_from_a_website%" is *PARTIALLY* in me list... the full line item in me list is actually "%whole_line_from_string_list%" but shhhh, don't tell them... I think they're after me Lucky Charms!
  EndIf

EndIf

End>test_the_string_and_display_result
jpuziano

Note: If anyone else on the planet would find the following useful...
[Open] PlayWav command that plays from embedded script data
...then please add your thoughts/support at the above post - :-)

Post Reply
Sign up to our newsletter for free automation tips, tricks & discounts