Filtering a string with 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

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

Re: Filtering a string with a string

Post by Klaus » Tue Apr 25, 2017 10:12 pm

Hi Zood,
Zood wrote:I have set my URL into a variable: CONTENTS
I have added a line to the URL and the line itself shows itself on a field in which i have put the contents of CONTENTS: So far so good!
not sure I get what you are talking about? Let me see:
So you have put the contents of the file "Contents.csv" into a variable, right?
Then you have put the varialbe into a field named "CONTENTS".
To what URL have you added a line? Or do you mean the field?
And what on earth means " the line itself shows itself on a field..."?

OK, next:
Zood wrote:

Code: Select all

...
put the hilitedline of field "CONTENTS" into tContents   ## puts the line-number of the hilitedline into tContents
delete line tContents of field "CONTENTS"      ## deletes the line which was highlighted from the selected field only, not from the URL
put line tContents of field "CONTENTS" into tContents3
...
The result of these commands will probably surprise you, please think about this again.
Hint: it has to to with the order of the commands. :D

If field "contents" now has the content that you would also to be in the file "Contents.csv",
then why not just write the field back into the file?

Code: Select all

...
put fld "CONTENTS" into url ("file:" & specialFolderPath("desktop") & "/CONTENTS.csv")
## Done!
...
Next:
Zood wrote:

Code: Select all

...
repeat for each line tContents2 in url ("file:" & specialFolderPath("desktop") & "/CONTENTS.csv")
      if tContents2 = tContents3 then
         delete line tContents2 in url ("file:" & specialFolderPath("desktop") & "/CONTENTS.csv")
      end if
end repeat
...
1. Do not use a repat loop on a FILE.
Put it into a variable first, compute the variable and then write the variable back to file, this is MUCH faster!

2. tContents2 contains the CONTENT Of that line and NOT the line number, so this does not work of course:

Code: Select all

...
delete line tContents2 in url...
...
I would do something like this

Code: Select all

...
put line tContents of field "CONTENTS" into tContents3
put url ("file:" & specialFolderPath("desktop") & "/CONTENTS.csv") into tFile
## Since repeat for each does NOT supply any line number, we need to maintain a counter by ourselves
put 0 into tCounter
repeat for each line tContents2 in tFile
     add 1 to tCounter
     if tContents2 = tContents3 then
        ## Found the line, no need to progress!
        exit repeat
     end if
end repeat
## Now delete the line of the variable and...
delete line tCounter of tFile
### write it back to file.
put tFile into url("file:" & specialFolderPath("desktop") & "/CONTENTS.csv")
...
Best

Klaus

Zood
Posts: 37
Joined: Mon Apr 24, 2017 7:17 pm

Re: Filtering a string with a string

Post by Zood » Thu Apr 27, 2017 12:30 am

Hey Klaus,

I saw the order of the lines indeed after I had posted them, not the way it is supposed to be :D
I got the part to delete the line! worked like a charm. Thanks!
Is there also a way to put specific parts of a line into a variable?
For example:
Line X: a ; b ; c ; d ; e ; f.
Put the part of line X from the second ";" to the third ";" ( and thus part C) into tC
put tC into field "C"

Thanks alot!

AxWald
Posts: 578
Joined: Thu Mar 06, 2014 2:57 pm

Re: Filtering a string with a string

Post by AxWald » Thu Apr 27, 2017 10:08 am

Hi,
Zood wrote:Is there also a way to put specific parts of a line into a variable?
For example:
Line X: a ; b ; c ; d ; e ; f.
Put the part of line X from the second ";" to the third ";" ( and thus part C) into tC
put tC into field "C"
Try:

Code: Select all

   set the itemdelimiter to ";"
   put item 3 of line x into fld "C"
Have fun!
All code published by me here was created with Community Editions of LC (thus is GPLv3).
If you use it in closed source projects, or for the Apple AppStore, or with XCode
you'll violate some license terms - read your relevant EULAs & Licenses!

Zood
Posts: 37
Joined: Mon Apr 24, 2017 7:17 pm

Re: Filtering a string with a string

Post by Zood » Mon May 01, 2017 10:34 pm

Thnx AxWald!

Another question:
Is there a standard function to put the last 5 lines of a certain variable into another variable?
for example:
field X:
a
b
c
d
e
f
g

put the last five lines of field "X" into tLines
put tLines into field "Y"
field Y:
c
d
e
f
g

Thanks in advance!

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

Re: Filtering a string with a string

Post by Klaus » Mon May 01, 2017 10:55 pm

Hi Zood,

no standard function avaialable, but we can count from backwards in LC, which comes quite handy in your case :D
...
put line -5 to -1 of YourVariableHere into fld "the other one..."
...


Best

Klaus

Post Reply