Source language: Translate to:

SearchStr clarification

Questions about using NeoBook's scripting language

Moderator: Neosoft Support

SearchStr clarification

Postby DantesGame » Mon Feb 09, 2015 8:20 pm

Hi, all.

I've been playing with variations of this code for a couple of nights now trying to get it to work. It seems like this should be a simple function but it's escaping me. I have a text file (names.txt) that I want to build a simple search function for. Text entry field to enter the search term and a button to execute the search code, spit the results back in an AlertBox. Easy, right? I can't make it work, however.

Here's where I landed on my most recent attempt:

. Let's read the file contents and store then in a Variable
FileRead "[PubDir]\names.txt" "All" "[FileContents]"

.Let's search the Variable above for the search term typed in our text entry field and store that in its own variable
SearchStr "[SearchTerm]" "[FileContents]" "[MySearch]" ""

.Let's display the search results in an AlertBox
AlertBox "Your Results" "[MySearch]"

What I get back is "27" (without the quotation marks)... This puts my thinking on the path of an array position or place in the file where the word/string falls. Am I on the right track with that gut feeling or is there a fundamental step I'm missing in the search process?

Thank you for any assistance.
DantesGame
 
Posts: 29
Joined: Sun Jun 08, 2008 1:15 pm

Re: SearchStr clarification

Postby Gaev » Mon Feb 09, 2015 9:25 pm

DantesGame:

Looks like it is working ... i.e. it found a string of characters within the text contained in variable [FileContents] ... that matched the contents of the variable [SearchItem] ... at position 27 within the text contained in [FileContents].

It is unclear what exactly you were trying to accomplish i.e. when you say spit the results back in an AlertBox ... what exactly (if not 27) were you expecting to be spit ?

Perhaps you can explain with an example like ...

[FileContents] contains "Jack and Jill went up the hill"
[SearchItem] contains "went"

... now what exactly what do you want to search and spit back ?
User avatar
Gaev
 
Posts: 3716
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: SearchStr clarification

Postby DantesGame » Mon Feb 09, 2015 9:49 pm

Hi Gaev,

lol.. Yes, sorry, I should have been more precise with what result I was looking for. I want to display the search term and/or text around it.

If the results are found, I want to display the actual results found (i.e. all instances of the name Billy).

So for example, if my names.txt contains the following:
Billy Smith
Brenda Jones
Sam Johnson
Billy White
Linda Brown

...and I enter the search term Billy, I would like the results to come back with Billy Smith, Billy White.

Does that make sense?
DantesGame
 
Posts: 29
Joined: Sun Jun 08, 2008 1:15 pm

Re: SearchStr clarification

Postby Gaev » Tue Feb 10, 2015 6:12 am

DantesGame:

As long as the file is not humongous i.e. it does not take too long to read one line (of the file) at a time, here is one way ...
Code: Select all
SetVar "[NamesFile]" "![PubDir]\names.txt"

... determine number of lines to search
FileLen "![NamesFile]" "[NamesLines]"

... clear variable to contain matching lines
SetVar "[SearchResults]" ""

... loop through all lines in the file
Loop "1" "[NamesLines]" "[thisLine]"
   FileRead "![NamesFile]" "[thisLine]" "[thisLineText]"
   SearchStr "[SearchTerm]" "[thisLineText]" "[SearchPosition]" ""
   If "[SearchPosition]" ">" "0"
      ...found a match; add to [SearchResults]
      SetVar "[SearchResults] "[SearchResults][thisLineText][#13][#10]"
   EndIf
EndLoop

AlertBox "Matching Lines" "[SearchResults]"

Note: Above code has not been tested by me ... may contain typo errors

If this is taking too long, a more efficient way would be to ...

- read all lines into a variable in one shot (like in your original post)
- use StrParse to split it (using delimiter of [#13][#10]) into an array variable
- loop through each array item to look for matches
User avatar
Gaev
 
Posts: 3716
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: SearchStr clarification

Postby DantesGame » Tue Feb 10, 2015 10:56 am

Hi Gaev,

Thanks for the pointers. I got a little closer late last night and even stumbled upon the parse vs. file read bit in this post:
viewtopic.php?f=2&t=18081&hilit=parsing+is+better+than+reading

My code is below and I know which line is wrong but I don't know which Action Command to use in place of FileRead. See example below.

Code: Select all
FileToVar "![PubDir]\names.txt" "[NamesFile]"
StrParse "[NamesFile]" "[#13][#10]" "[NamesArray]" "[NumberLines]"
SetVar "[SearchResults]" ""
Loop "1" "[NumberLines]" "[thisLine]"
--> FileRead "[NamesFile]" "[thisLine]" "[thisLineText]" <-- This line isn't right but I can't figure which Action Command to use.
SearchStr "[SearchTerm]" "[thisLineText]" "[SearchPosition]" ""
   If "[SearchPosition]" ">" "0"
      ...found a match; add to [SearchResults]
      SetVar "[SearchResults]" "[SearchResults][thisLineText][#13][#10]"
   EndIf
EndLoop

AlertBox "Your Results" "[SearchResults]"


Since we're now storing the contents of the file in a Variable and are parsing the contents into an array, we no longer need to go back and do a FileRead--correct? (since we've created an array of the content). So how do I now access the array contents to do my SearchStr..?
DantesGame
 
Posts: 29
Joined: Sun Jun 08, 2008 1:15 pm

Re: SearchStr clarification

Postby Gaev » Tue Feb 10, 2015 12:45 pm

DantesGame:

Since we're now storing the contents of the file in a Variable and are parsing the contents into an array, we no longer need to go back and do a FileRead--correct? (since we've created an array of the content). So how do I now access the array contents to do my SearchStr..?


When you do this ...
Code: Select all
StrParse "[NamesFile]" "[#13][#10]" "[NamesArray]" "[NumberLines]"
... you will end up with variables like [NamesArray1], [NamesArray2] , , , [NamesArrayZ] (where Z is the number of lines in the file).

In order to access each array variable inside the loop, you need to use something called "nested variables" ... like this ... [NamesArray[thisLine]] ... so your code should look like ...
Code: Select all
FileToVar "![PubDir]\names.txt" "[NamesFile]"
StrParse "[NamesFile]" "[#13][#10]" "[NamesArray]" "[NumberLines]"
SetVar "[SearchResults]" ""
Loop "1" "[NumberLines]" "[thisLine]"
   SearchStr "[SearchTerm]" "[NamesArray[thisLine]]" "[SearchPosition]" ""
   If "[SearchPosition]" ">" "0"
      ...found a match; add to [SearchResults]
      SetVar "[SearchResults]" "[SearchResults][NamesArray[thisLine]][#13][#10]"
   EndIf
EndLoop

AlertBox "Your Results" "[SearchResults]"
User avatar
Gaev
 
Posts: 3716
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: SearchStr clarification

Postby DantesGame » Tue Feb 10, 2015 1:29 pm

Oh, that's bloody brilliant! I get it.

To recap: We no longer need to do a FileRead... we're pulling directly from the array so we use SearchStr to search the array *for the search term*--which winds up in [thisLine]... is that correct?

The only part that has me scratching my head is the use of nested variables. I couldn't find anything in the Help file for NeoBook about those.

Thanks for your help, Gaev. It's appreciated.
DantesGame
 
Posts: 29
Joined: Sun Jun 08, 2008 1:15 pm

Re: SearchStr clarification

Postby Gaev » Tue Feb 10, 2015 2:02 pm

DantesGame:
To recap: We no longer need to do a FileRead...
You no longer need to read the file repeatedly "one line at a time" ... but FileToVar is similar to FileRead with the "All" option.

we're pulling directly from the array so we use SearchStr to search the array *for the search term*--which winds up in [thisLine]... is that correct?
Not quite ... in the array version of the solution, you have ...
Code: Select all
SearchStr "[SearchTerm]" "[NamesArray[thisLine]]" "[SearchPosition]" ""
... so you are searching against the array item [NamesArray[thisLine]] ... where [thisLine] will be iterated from 1 to whatever the number of of array items that were created as a result of the StrParse.

The only part that has me scratching my head is the use of nested variables. I couldn't find anything in the Help file for NeoBook about those.
It is a very powerful capability of NeoBook actions ... not found in most other scripting languages ... the way it works is that the "inner variable' is resolved first ... this value is appended to form the "outer variable" ... which is the resolved just like a regular variable ... so if [thisLine] contained the number 6 ... [NamesArray[thisLine]] would resolve to [NamesArray6] ... and then the value of [NamesArray6] would be used in the command.
User avatar
Gaev
 
Posts: 3716
Joined: Fri Apr 01, 2005 7:48 am
Location: Toronto, Canada

Re: SearchStr clarification

Postby DantesGame » Tue Feb 10, 2015 7:29 pm

Gaev wrote:DantesGame:

The only part that has me scratching my head is the use of nested variables. I couldn't find anything in the Help file for NeoBook about those.
It is a very powerful capability of NeoBook actions ... not found in most other scripting languages ... the way it works is that the "inner variable' is resolved first ... this value is appended to form the "outer variable" ... which is the resolved just like a regular variable ... so if [thisLine] contained the number 6 ... [NamesArray[thisLine]] would resolve to [NamesArray6] ... and then the value of [NamesArray6] would be used in the command.


That's really cool. I appreciate the extra detail and clarification on the other points. So with the nested variables sounds like something that would come in especially handy if you were reading from a CSV file...or is that a whole other monster?

I want to get good at these basics first and then I'm going to possibly try my hand at adding on NeoBookDBPro. I have a larger project in mind but need to walk before I can run.

Thanks again!
DantesGame
 
Posts: 29
Joined: Sun Jun 08, 2008 1:15 pm


Return to NeoBook Action Commands

Who is online

Users browsing this forum: No registered users and 2 guests

cron