Searching for all instances of a string

Got a LiveCode personal license? Are you a beginner, hobbyist or educator that's new to LiveCode? This forum is the place to go for help getting started. Welcome!

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Searching for all instances of a string

Post by stoavio » Tue Jul 16, 2013 4:23 pm

Hey guys,

I am struggling a bit writing some code to search a field for a string. Basically, I have content being held in this field from a file that a user selected. I opened it, read it, and now I want to search it for "APPROVED NO". For each instance of "APPROVED NO" I find I want to put into a variable and count the number of instances. The end goal is to be able to use this program to say how many times a credit card transaction was rejected.

I know LiveCode's text manipulation ability is supposed to be easy and powerful but it confuses the hell out of me compared to using AutoIt.

Code: Select all

on mouseUp
   answer file "Choose your STL file:"
   put it into fld "Path" //this just shows the user the path they selected
   open file(fld "Path") for read //open file
   read from file (fld "Path") at 1 until EOF //read file
   put it into fld "File" //put contents of file into field for user to see
  end mouseUp
I've got the content loaded I want to search. Now, I just don't know how to search it effectively. The first search I need to perform on it (as mentioned above) is for "APPROVED NO".

I know i need to use a loop so I was thinking about saying

Code: Select all

repeat for number of lines in fld "File"
 ...
end repeat
And putting my search code in there. Any ideas?

Thanks!

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Searching for all instances of a string

Post by dunbarx » Tue Jul 16, 2013 4:52 pm

I wrote a function a long time ago that replicated an old Hypercard external from a man named Rinaldi. That is neither here nor there.

Do this: answer revFullFind(yourText,stringToFind,"false")

Put this into a script"

Code: Select all

function revFullFind tText,tFind,exactly --RETURNS LINE NUMBER & "," & WORD NUMBER(S)
   put 0 into counter
   switch exactly
      case "true"
      case empty
         repeat for each line tLine in tText
            add 1 to counter
            if tFind = tLine then
               put counter & return after tResult
            end if
         end repeat
         break
      case "false"
         repeat for each line tLine in tText
            add 1 to counter
            if tFind is in tLine then
               repeat with y = 1 to the number of words in tLine
                  if word y of tLine = tFind then put y & "," after temp
               end repeat
               delete last char of temp
               put counter & "," & temp & return after tResult
               put "" into temp
            end if
         end repeat
         break
   end switch
   return tResult
end revFullFind
You can modify this as you want. There are other ways to count the instances of a string in another string, but this should give you something to start with,

Craig Newman

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7389
Joined: Sat Apr 08, 2006 8:31 pm
Contact:

Re: Searching for all instances of a string

Post by jacque » Tue Jul 16, 2013 5:10 pm

If all you want is the number of instances:

Code: Select all

filter tData with "*APPROVED NO*"
put the number of lines in tData into tNos
If you want the actual lines that contain the string, just use what's left in tData.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Searching for all instances of a string

Post by dunbarx » Tue Jul 16, 2013 5:22 pm

Jacque.

This would undercount if more than one instance was contained in a single line.

Craig

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Searching for all instances of a string

Post by Klaus » Tue Jul 16, 2013 5:26 pm

Hi stoavio,

1. you might want to use the shorter URL syntax for file access:

Code: Select all

on mouseUp
   answer file "Choose your STL file:"

   ## IT will change when you least exspect it, so use IT only ONCE 
   ## and then put IT into a variable! ;-)
   put it into tFile

   ## User MAY cancel :-)
   if tFile = empty then
     exit mouseup
   end if
   put tFile into fld "Path" //this just shows the user the path they selected
   put url("file" & tFile) into fld "File" //put contents of file into field for user to see
  end mouseUp
If the term "APPROVED NO" will only appear ONE TIME in ONE LINE, you could use filter:
...
put fld "File" into tList

## Case 1: The term is NOT at the beginning NOR at the end of a line
filter tList with "*APPROVED NO*"

## Case 2. The term is always at the beginning of each line
filter tList with "APPROVED NO*"

## Case 3: The term is at the end of a line:
filter tList with "*APPROVED NO"

## Now tList will only contain all lines with "APPREOVED NO" in them!
...
In case tList is a TAB delimited text list, you can easily carry on with the remaining data.
You get the picture :-)


Best

Klaus

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Searching for all instances of a string

Post by stoavio » Tue Jul 16, 2013 5:45 pm

Thanks for the help everyone! I will experiment with filtering.

@Klaus: Using the shorter syntax you mentioned doesn't achieve the result I am after. Instead of opening and reading the contents of a file to a field, it simply displays the path to the file selected.

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Searching for all instances of a string

Post by stoavio » Tue Jul 16, 2013 5:53 pm

Also, can someone explain to me the naming convention of variables? Sometimes I see eg: tFile, gFile, lFile.

What do the lowercase letters before the variable name represent and when do you use them?

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Searching for all instances of a string

Post by dave_probertGA6e24 » Tue Jul 16, 2013 6:19 pm

Hi,

Klaus is actually correct (as usual!), but made a spelling mistake in the example:

Code: Select all

put url("file" & tFile) into fld "File"
should have a Colon after the file part:

Code: Select all

put url("file:" & tFile) into fld "File"
Look up the URL command in the dictionary.

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Searching for all instances of a string

Post by Klaus » Tue Jul 16, 2013 6:22 pm

Hi stavio,

...
## tFile contains the filepath to the selected file!
##This line will put the filepath to the selected file into field "Path"
put tFile into fld "Path" //this just shows the user the path they selected

## And this line will put the CONTENT of the selected file into field "File":
put url("file:" & tFile) into fld "File" //put contents of file into field for user to see
...
This DOES indeed work, at least it did for me the last 15 years :-)

If it doesn't work for you, add this line and see if you see any error dialog:
...
put url("file:" & tFile) into fld "File"
## THE RESULT will be empty on success:
if the result <> EMPTY then
answer the result
end if
...

Concerning the variable prefixes, check this interesting article, which cover the topic :-)
http://www.fourthworld.com/embassy/arti ... style.html
Scroll down to "Naming conventions"...

And do also read the rest! :-D


Best

Klaus

Klaus
Posts: 14177
Joined: Sat Apr 08, 2006 8:41 am
Contact:

Re: Searching for all instances of a string

Post by Klaus » Tue Jul 16, 2013 6:23 pm

Oh yes, sorry, forgot the colon after "file:".

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Searching for all instances of a string

Post by stoavio » Tue Jul 16, 2013 8:12 pm

Ahh thanks for clearing that up Klaus.

One more question. The file I am "parsing" with this program has a bunch of transactions that each look like this:

Code: Select all

BEGIN
    TYPE       AUTHORIZE
    DOB        20130225
    FOHFEATS   10000000000000000000000000000000
    TERMCAPS   00000000000000000000000000000000
    FOHVERSION 6.7.25.3161
    ACTIONCODE 000
    DATE       20130225
    TIME       10:48:09
    TERMINAL   2
    EMPLOYEE   100640 Lauren 
    TABLE      JAKE
    CHECK      20004
    PAYMENT    20002
    AUTHAMT    7.71
    BATCHAMT   7.71
    CARDTYPE   VISA
    REF        95573017
    APPROVED   YES
    AUTH       084811
    FILENO     10000002
    PROCNO     16
    PINDEX     1
    TENDER_NUM 25
    CRCY       840
    REVID      1
    REVNAME    Dining Room
    MDMONLY    FALSE
END
I am trying to use the select command to select everything between "BEGIN" and "END" but am not having luck with:

Code: Select all

select after word "begin" to word "end"


I know I'm doing it wrong but I haven't been able to find the right way to do it by searching the dictionary.

dave_probertGA6e24
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 328
Joined: Mon Dec 05, 2011 5:34 pm
Contact:

Re: Searching for all instances of a string

Post by dave_probertGA6e24 » Tue Jul 16, 2013 11:16 pm

Read the dictionary regarding the command Select.

It doesn't select a range of text in a variable, only in fields.

You would either need to put the content of the variable into a field and then do the select, or use another method to set the start and end points for what you want to select.

You could probably use a filter command to get the locations of all lines with BEGIN and then loop through that list and use the Offset function to get the location of the next line with END. Then you can grab that chunk of text off into another variable (or array, or whatever).

There are many ways of doing text manipulation in LC, but some of them are less obvious than others, and for some of them you REALLY need to read the dictionary to fully understand! I know I do - often!

Cheers,
Dave
Coding in the Sun - So much Fun.
Visit http://electronic-apps.info for released App information.

stoavio
Posts: 43
Joined: Sun Jun 30, 2013 2:09 am

Re: Searching for all instances of a string

Post by stoavio » Wed Jul 17, 2013 12:22 am

Thanks Dave. I did end up finding a way to do it. I guess I tend to assume there are easier ways to do things most of the time but in this case, the method for this particular manipulation wasn't quite as straight-forward as I assumed it would be.

Thanks again all for the assistance.

Andy01
Posts: 14
Joined: Wed Dec 22, 2021 3:26 am

Re: Searching for all instances of a string

Post by Andy01 » Tue Aug 23, 2022 5:18 pm

put CountText("APPROVED NO") into howmany

function CountText data
find whole data
if the result = "not found" then return 0
put the foundChunk into firstPos
put 1 into counter
repeat
find whole data
put the foundChunk into secPos
if firstPos = secPos then exit repeat
add 1 to counter
end repeat
return counter
end CountText

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 10305
Joined: Wed May 06, 2009 2:28 pm

Re: Searching for all instances of a string

Post by dunbarx » Tue Aug 23, 2022 6:28 pm

Andy01

Please put code segments within the code tags "</>". The icon is just above the text field. Makes reading a lot easier.

Craig

Post Reply