Identifying occurences of specified text in one substack and report to another substack

Anything beyond the basics in using the LiveCode language. Share your handlers, functions and magic here.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

kresten
Posts: 152
Joined: Tue Sep 30, 2008 3:01 pm
Location: Copenhagen
Contact:

Identifying occurences of specified text in one substack and report to another substack

Post by kresten » Tue Apr 02, 2019 3:34 pm

I am working to transform a process where results were delivered to an external file to a format were results now instead shall come to another substack
Briefly: 2 substacks:
a) "Phenomenalog", accumulating daycards, with textfield, for users diary notes
b) "Parsings" with two fields :
i) "searching" & ii) "occurrences"
The handler "phenoparse" is placed in the script of stack "Phenomenalog".
It permits user to specify a search-term and shall then scan the texts of all daycards identifying lines where the specified word(s) /name/number appears and report them
as ocurrences on a new card in the "Parsings" stack.
I attach the script of the handler in the hope that someone can suggest how to repair it.

< on phenoparse
global phenodate,cue,start,hitcard
ask "Which word shall be searched?"
if it is empty then
exit phenoparse
else
put it into cue
end if
ask "How many lines before ?" with "0"
if it is cancel then
exit phenoparse
end if
put it into avant
ask "How many lines after ?" with "0"
if it is cancel then
exit phenoparse
end if
put it into apres
open stack "Parsings"
new card
open field "searchings"
put "Parsed for" && cue && phenodate after field "searchings"of stack "Parsings"
close stack "Parsings"
go first card of stack "Phenomenalog"
find string cue in field "TextField"
if the result is "not found" then
answer "Cannot find this word"
exit phenoparse
else
put short name of this card && the foundchunk into start
put short name of this card into hitcard
put "<p>" & start & hitcard &"</p>" into field “occurrences” of last card of stack "Parsings"
put "<p>" & short name of this card & "</p>" into field “occurrences” of last card of stack "Parsings"
-- put the htmltext of line (word 2 of the foundline) - avant to (word 2 of the foundline) + apres of field "Textfield" after field “occurrences” of stack pParsings
end if
repeat
set cursor to busy
find string cue in field "TextField"
if short name of this card && the foundchunk is start then
exit repeat
end if
if short name of this card is not hitcard then
put short name of this card into hitcard
put "<p> ----------------------</p>" after field “occurrences” of stack “Parsings”
put "<p>" & short name of this card & "</p>" after field “occurrences” of stack “Parsings”
end if
put the htmltext of line (word 2 of the foundline) - avant to (word 2 of the foundline) + apres of field "Textfield" after field “occurrences” of stack “Parsings”
end repeat
go last card
put return & phenodate && the time && "Parsed:" && cue after field "TextField"
show stack "Parsings"
end phenoparse />

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Identifying occurences of specified text in one substack and report to another substack

Post by dunbarx » Tue Apr 02, 2019 4:45 pm

Hi again.

This is the same as your post in the "Experienced Developers" pane, no?

I wrote there, though you never commented if I was anywhere near the problem:
Hi,

Please, please frame code offerings within the code tags (</>). It makes it much easier to see what is going on.

I glanced at your handler. Is this correct?

Code: Select all

if short name of this card && the foundchunk is start then
See the line properly "tagged"? And does this make sense?

Craig

kresten
Posts: 152
Joined: Tue Sep 30, 2008 3:01 pm
Location: Copenhagen
Contact:

Re: Identifying occurences of specified text in one substack and report to another substack

Post by kresten » Thu Apr 04, 2019 7:26 pm

I tried to give a more precise title to the topic and a clearer description of the setup. Also the version of the script was slightly modified. I am too ignorant to find help in Your question about a single line in the handler .
I hope you or other visitors could suggest ways to elaborate the script to avoid error(s?)

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Identifying occurences of specified text in one substack and report to another substack

Post by dunbarx » Thu Apr 04, 2019 8:04 pm

Kresten,

The line I mentioned is about 75% down the handler. I only glanced at your code, and wanted to make sure this line, which seems just a little odd, was what you meant.

Was an error thrown, or did the handler just not work?

In any case, in the future, do place code in code brackets.

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Identifying occurences of specified text in one substack and report to another substack

Post by bogs » Thu Apr 04, 2019 8:59 pm

Hi again kresten :D

In case you aren't sure what Craig means by putting your above code into a code block, look at the top of your post with the code in it for this pencil (right hand side)
Selection_001.png
Edit the post pencil...
Selection_001.png (7.59 KiB) Viewed 7899 times
After you click on that, highlight all your code in the post, and click on the button that looks like [</>] -
Selection_002.png
It is all in the code...
...and you'll wind up with something like this once you click on "submit"

Code: Select all

< on phenoparse
global phenodate,cue,start,hitcard
ask "Which word shall be searched?"
if it is empty then
exit phenoparse
else
put it into cue
end if
ask "How many lines before ?" with "0"
if it is cancel then
exit phenoparse
end if
put it into avant
ask "How many lines after ?" with "0"
if it is cancel then
exit phenoparse
end if
put it into apres
open stack "Parsings"
new card
open field "searchings"
put "Parsed for" && cue && phenodate after field "searchings"of stack "Parsings"
close stack "Parsings"
go first card of stack "Phenomenalog"
find string cue in field "TextField"
if the result is "not found" then
answer "Cannot find this word"
exit phenoparse
else
put short name of this card && the foundchunk into start
put short name of this card into hitcard
put "<p>" & start & hitcard &"</p>" into field “occurrences” of last card of stack "Parsings"
put "<p>" & short name of this card & "</p>" into field “occurrences” of last card of stack "Parsings"
-- put the htmltext of line (word 2 of the foundline) - avant to (word 2 of the foundline) + apres of field "Textfield" after field “occurrences” of stack pParsings
end if
repeat
set cursor to busy
find string cue in field "TextField"
if short name of this card && the foundchunk is start then
exit repeat
end if
if short name of this card is not hitcard then
put short name of this card into hitcard
put "<p> ----------------------</p>" after field “occurrences” of stack “Parsings”
put "<p>" & short name of this card & "</p>" after field “occurrences” of stack “Parsings”
end if
put the htmltext of line (word 2 of the foundline) - avant to (word 2 of the foundline) + apres of field "Textfield" after field “occurrences” of stack “Parsings”
end repeat
go last card
put return & phenodate && the time && "Parsed:" && cue after field "TextField"
show stack "Parsings"
end phenoparse />

It will make your posts with long runs of code in them easier for everyone else to read.

As far as the code you posted goes, I'm not sure why you have this arrow in the first line -
< on phenoparse
or this in the last line
end phenoparse />
Since they are outside the actual handler, though, I'm not sure they affect it either, just was puzzling to look at.
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Identifying occurences of specified text in one substack and report to another substack

Post by dunbarx » Thu Apr 04, 2019 11:20 pm

Bogs.

My code block icon looks like this: </>

Is this a Mac thing?


Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Identifying occurences of specified text in one substack and report to another substack

Post by bogs » Fri Apr 05, 2019 3:11 am

dunbarx wrote:
Thu Apr 04, 2019 11:20 pm
My code block icon looks like this: </>
Is this a Mac thing?
It is an ascii art thing, I wanted to include the button like surrounding, but now I realize that to get the (somewhat) fuller effect, I needed to include the bottom [</>] :D
Image

kresten
Posts: 152
Joined: Tue Sep 30, 2008 3:01 pm
Location: Copenhagen
Contact:

Re: Identifying occurences of specified text in one substack and report to another substack

Post by kresten » Fri Apr 12, 2019 6:19 pm

Hi Dunbarx and bogs
I have been away for some days, now trying to understand your advices.
the line "if short name of this card && the foundchunk is start then" was meant to put an end to the scanning when the first occurrence is re-met

Code: Select all

on phenoparse
   global phenodate,cue,start,hitcard
   ask "Which word shall be searched?"
   if it is empty then
      exit phenoparse
   else
      put it into cue
   end if
   ask "How many lines before ?" with "0"
   if it is cancel then
      exit phenoparse
   end if
   put it into avant
   ask "How many lines after ?" with "0"
   if it is cancel then
      exit phenoparse
   end if
   put it into apres
   open stack "Parsings"
   new card
   open field "searchings"
   put "Parsed for" && cue && phenodate after field "searchings"
   lock screen
   set lockmessages to true
   go first card of stack "Phenomenalog"
   find string cue in field "TextField"
   if the result is "not found" then
      answer "Cannot find this word"
      exit phenoparse
   else
      put short name of this card && the foundchunk into start
      put short name of this card into hitcard
      close stack "Parsings"
      put "<p>" & start & hitcard "</p>" after field “occasions” of stack “Parsings”
      put "<p>" & short name of this card & "</p>" after field “occasions”of stack Parsings
   end if
   repeat
      set cursor to busy
      find string cue in field "TextField"
      if short name of this card && the foundchunk is start then
         exit repeat
      end if
      if short name of this card is not hitcard then
         put short name of this card into hitcard
         put "<p> ----------------------</p>" after field “occasions” of stack “Parsings”
         put "<p>" & short name of this card & "</p>" after field “occasions” of stack “Parsings”
      end if
   end repeat
   go last card
   put return & phenodate && the time && "Parsed:" && cue after field "TextField"
   show stack "Parsings"
end phenoparse

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Identifying occurences of specified text in one substack and report to another substack

Post by dunbarx » Fri Apr 12, 2019 7:49 pm

Hi.
"if short name of this card && the foundchunk is start then
It is just an odd construction, and I wanted to make sure it was what you intended. The line of code is in and of itself sound.

It would concatenate two strings. So if the short name of the card is "myData", and the foundChunk is
"char 37 to 43 of field 10" then you would be saying:

Code: Select all

if "myData char 37 to 43 of field 10" = start then...
Do you see? This string looks odd to me, though no error would be thrown.

Using the foundChunk is itself an issue; it is fragile. You must always save it the instant you have it, because it will be discarded by LC if you do almost anything else subsequently. That is for later, though.

But is that what you had in mind? I bet not.

Craig

[-hh]
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2262
Joined: Thu Feb 28, 2013 11:52 pm
Location: Göttingen, DE

Re: Identifying occurences of specified text in one substack and report to another substack

Post by [-hh] » Sat Apr 13, 2019 9:19 am

Please note that "start" is a reserved word.
Using it as variable name may create problems in some situations.
shiftLock happens

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Identifying occurences of specified text in one substack and report to another substack

Post by dunbarx » Sat Apr 13, 2019 1:38 pm

Hermann is correct.

You can use a reserved word like "start":

Code: Select all

on mouseUp
   put "XYZ" into start
   if start = "XYZ" then answer char 2 of start
end mouseUp
But just don't. And you do not want to have to look up each word when you are writing your code to make sure it is not reserved, so get into the habit of using variable names like "tStart" or "inTheBeginning".

But we still have issues with your code, no?

Craig

Klaus
Posts: 13824
Joined: Sat Apr 08, 2006 8:41 am
Location: Germany
Contact:

Re: Identifying occurences of specified text in one substack and report to another substack

Post by Klaus » Sat Apr 13, 2019 2:29 pm

Well, you just need to look at your script (hit TAB from time to time if neccessary)!
All reserved words are somehow colorized, aren't they? 8)

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Identifying occurences of specified text in one substack and report to another substack

Post by bogs » Sat Apr 13, 2019 2:31 pm

dunbarx wrote:
Sat Apr 13, 2019 1:38 pm
And you do not want to have to look up each word when you are writing your code to make sure it is not reserved, so get into the habit of using variable names like "tStart" or "inTheBeginning".
While I absolutely agree with starting variables with anything that indicates their scope, you can certainly find out if a word is reserved or not far easier than looking it up manually.

Note the picture below -
StepOsTesters [Running] - Oracle VM VirtualBox_001.png
reserved words....
Words that are reserved such as commands, functions, keywords, etc will usually tip you they are reserved by either their color or/and font style (bold, italic, etc), so right off the bat you know it is reserved.

Messages are not colored or styled, but if you click on the 'Documentation' tab at the bottom of the editor, you will see if it is a message as soon as you finish typing.

But Craig is dead on with his suggestion, until you get to tExt for a variable :D
Image

dunbarx
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9648
Joined: Wed May 06, 2009 2:28 pm
Location: New York, NY

Re: Identifying occurences of specified text in one substack and report to another substack

Post by dunbarx » Sun Apr 14, 2019 8:58 pm

Bogs.

I have NEVER ever written this before. I hope you are gratified and impressed. You wrote:
until you get to tExt for a variable
OK, ready?

LOL

Craig

bogs
Posts: 5435
Joined: Sat Feb 25, 2017 10:45 pm

Re: Identifying occurences of specified text in one substack and report to another substack

Post by bogs » Sun Apr 14, 2019 9:27 pm

dunbarx wrote:
Sun Apr 14, 2019 8:58 pm
Bogs.
I have NEVER ever written this before. I hope you are gratified and impressed. You wrote:
until you get to tExt for a variable
OK, ready?

LOL

Craig
I am impressed, even though it isn't mine :D

I got it reading the archive mailing lists, either from Jacque, or someone else responding to her. It was funny enough to stick with me for the last year +
Image

Post Reply

Return to “Talking LiveCode”