URL and "Contains" on multiple items

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

URL and "Contains" on multiple items

Post by Nakia » Wed Mar 07, 2012 10:27 am

Hi Again,

trying to search a URL and see if it contains 2 strings.
Using this but in the dictionary doesn't offer any assistance for looking for multiple strings.

If I look for just one item it seems to work just fine...

on checkIfExist
set the defaultFolder to specialFolderPath("Documents")
  if URL ("file:flights.dat") contains "1517" and "UNKNOWN"
then
answer "data exists" with "OK"
else
answer "all clear" with "OK"
end if

end checkIfExist

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

Re: URL and "Contains" on multiple items

Post by Klaus » Wed Mar 07, 2012 12:33 pm

hi Kia,

1. no need to set the defaultfolder every time, just create the complete filename.
2. You need to query both conditions as one complete query, see below
3. "answer" will always show the OK button automatically, so you can save some typing :)

Code: Select all

on checkIfExist
   ## First read the file into a variable
  put url("file:" & specialFolderPath("Documents") & "/flights.dat") into tFlightData

  ## Now check for both conditions
  if tFlightData contains "1517" AND tFlightData contains "UNKNOWN" then
        answer "data exists" with "OK"
   else
       answer "all clear" with "OK"
   end if
end checkIfExist
Best

Klaus

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Thu Mar 08, 2012 12:18 am

Can I apply this to reading the URL file itself In place of having to put it into a variable first?
Is it in efficient putting this formation into a variable over and over? I will be performing this task over and over as part
Of an error check in the App.


Also to add further question, I will be using the value of multiple option menus for the string to search against..is it best to place this into variables at handler start also?

Like this????

On checkData
--- read current selections of optionMenu button into variable
put pItemName of fld "button1" into tButtonOne
Put pItemName of fld "button2" into tButtonTwo
Put pItemName of fld "button3" into tButtonThree

--- read flights file into variable
put url("file:" & specialFolderPath("Documents") & "/flights.dat") into tFlightData
Question, will this over write all data in the variable next time it's called or will it add to what's already within?

--- check all three optionMenu buttons to see if data exists for this already in the tFlights variable
if tFlightsData contains tButtonOne and tFlightsData contains tButtonTwo and tFlightsData contains tButtonThree
Then
Answer "data exists"
Else
writeData
End if

End checkData

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

Re: URL and "Contains" on multiple items

Post by Klaus » Thu Mar 08, 2012 1:34 pm

Hi Kia,
Nakia wrote:Can I apply this to reading the URL file itself In place of having to put it into a variable first?
Is it in efficient putting this formation into a variable over and over? I will be performing this task over and over as part
Of an error check in the App.
well, this is a question of efficiency :D
Reading a file from the disk ONCE and storing it into a variable is definitively faster than reading the file EVERYTIME when needed!
Nakia wrote:Also to add further question, I will be using the value of multiple option menus for the string to search against..is it best to place this into variables at handler start also?
I think it will make more sense for you if you put the file into a GLOBAL variable when the stack opens!
this way, you can access the data erverwhere you need it!

Like this:

Code: Select all

on openstack

  # Need to declare it in every script you use the global!
  global gFlightData
  put specialFolderPath("Documents") & "/flights.dat" into tFlightDataFile

  ## Alway check!
  if there is a file tFlightDataFile then
      put url("file:" & specialFolderPath("Documents") & "/flights.dat") into gFlightData
  else
  put empty into dFlightData

   ## and(or optionally
   ## answer "No flight data saved"
 end if

  ## other openstack srtuff here...
end openstack
Then use this global in your script(s) like this:

Code: Select all

On checkData

   ## Tell LiveCode that yopu want to use the GLOBAL varuable 
   global gFlightData

   ---  read current selections of optionMenu button into variable
   put pItemName of fld "button1" into tButtonOne
   Put pItemName of fld "button2" into tButtonTwo
   Put pItemName of fld "button3" into tButtonThree

    --- check all three optionMenu buttons to see if data exists for this already in the tFlights variable
   if gFlightsData contains tButtonOne and gFlightsData contains tButtonTwo and gFlightsData contains tButtonThree then
      Answer "data exists"
    Else
       writeData
  End if
End checkData
## put url("file:" & specialFolderPath("Documents") & "/flights.dat") into tFlightData
## Question, will this over write all data in the variable next time it's called or will it add to what's already within?
PUT... INTO... will always replace the previous content of a variable/container!


Best

Klaus

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Fri Mar 09, 2012 12:04 am

Adding it as a global variable sounds great but that means it will only be populated once on stack open which will
Not be suffice. This file constantly changes so it will need to be re populated into the variable every time this function is called so using the the global probably won't work?

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

Re: URL and "Contains" on multiple items

Post by Klaus » Fri Mar 09, 2012 1:31 am

Hi Kia,

is it really neccessary to update the FILE everytime the data changes?
Can't you just update the global variable and write its content to the FILE when the app quits?


Best

Klaus

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Fri Mar 09, 2012 1:35 am

Yes it is unfortunately.
The app is a data collection form that writes the data to the file every time a new form is requested hence me wanting to check on new form submission that the data doesn't already exists.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Fri Mar 09, 2012 2:12 am

Or is there a way to Add data to the global variable without replacing the contents currently within?
Like add the text of fld "fld" to tGlobalVariable

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: URL and "Contains" on multiple items

Post by mwieder » Fri Mar 09, 2012 2:14 am

You mean as in

Code: Select all

put field "fld" after tGlobalVariable
?

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Fri Mar 09, 2012 3:44 am

So this will add the contents from field "fld" to the already existing contents of the global variable?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: URL and "Contains" on multiple items

Post by mwieder » Fri Mar 09, 2012 7:28 am

Yep.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Mon Mar 19, 2012 7:34 am

another question on the contains function.

I am comparing the contents of a variable with the contents of a field to see if a number exists. The field has multiple lines with three columns (tab separated)
and column 3 contains the number. now if the field had a value of 1234 in it and i compared a variable that contained 123 i would get a true return..

is there a way I can get the contains to look at the numbers as an entire string?

I.E. only return true if the fld contained exactly what was in the compared variable?

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Re: URL and "Contains" on multiple items

Post by mwieder » Mon Mar 19, 2012 5:18 pm

Code: Select all

set the wholeMatches to true
That should do the trick.

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Mon Mar 19, 2012 9:51 pm

Cool. Thanks, will try this out tonight....

Nakia
Posts: 425
Joined: Tue Feb 21, 2012 8:57 am

Re: URL and "Contains" on multiple items

Post by Nakia » Tue Mar 20, 2012 7:26 am

no good :?
still returning a match....

Post Reply