Handling situations where a requested card doesn't exist

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

Post Reply
glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Handling situations where a requested card doesn't exist

Post by glenn9 » Sun Nov 01, 2020 6:42 pm

Hi everyone,

I just wanted to check if there was a way of handling the situation of when issuing a 'go to Card 1' command but where 'card 1' doesn't exist?

i guess something along the lines of...

Code: Select all

go to card selectedText of me
if card selectedText of me 'doesn't exist' then...
(delete the selectedText of me)
Grateful for any advice.

Kind regards,

Glenn

jmburnod
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 2718
Joined: Sat Dec 22, 2007 5:35 pm
Location: Genève
Contact:

Re: Handling situations where a requested card doesn't exist

Post by jmburnod » Sun Nov 01, 2020 7:10 pm

Hi Glenn,
You may use

Code: Select all

if there is a cd "MyCard" then go to cd  "MyCard"
Kind regards
Jean-Marc
https://alternatic.ch

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Handling situations where a requested card doesn't exist

Post by glenn9 » Sun Nov 01, 2020 7:18 pm

Thanks Jean-Marc, this was very helpful. I've also discovered the command 'exists' in the dictionary

Code: Select all

 if exists(card selectedText of me) then
    go to card selectedText of me
Kind regards,

Glenn

FourthWorld
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 9842
Joined: Sat Apr 08, 2006 7:05 am
Location: Los Angeles
Contact:

Re: Handling situations where a requested card doesn't exist

Post by FourthWorld » Sun Nov 01, 2020 10:13 pm

FWIW "the result" will inform you if you attempted to go to a card that doesn't exist.
Richard Gaskin
LiveCode development, training, and consulting services: Fourth World Systems
LiveCode Group on Facebook
LiveCode Group on LinkedIn

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

Re: Handling situations where a requested card doesn't exist

Post by dunbarx » Mon Nov 02, 2020 5:16 am

It is hardly important in any useful sense, but there is *always* a card 1.

Craig

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Handling situations where a requested card doesn't exist

Post by glenn9 » Mon Nov 02, 2020 9:59 am

Thank you

I guess what I was wanting to achieve was to delete from a list the line that is no longer linked with a card (because the linked card had been previoulsy deleted), I want to to delete the 'orphaned' line by script.

I've now achieved this through:

Code: Select all

 on mouseup
 if exists(card selectedText of me) then
      go to card selectedText of me
   else
      put hilitedLine of field"List" into toDelete
      delete line toDelete of field"List"
      ...
However... enough is not enough, so I now want to delete the line from the list at the time of the linked card deletion and have got as far as this with code but struggling to somehow 'hilite' or 'indicate' the line in the list so it can be deleted...

Code: Select all

  -- sent from the card that is to be deleted, ie not card"Main"
   if field"List" of card "Main" contains toDelete then
           answer the hilitedtext of fld "Field" of card"Main" 
The code above works as expected but I'm struggling to code the subsequent 'delete the line' command
...any thoughts? (if any of this makes sense to third parites reading this...!)

regards,

Glenn

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

Re: Handling situations where a requested card doesn't exist

Post by dunbarx » Mon Nov 02, 2020 3:38 pm

Hi.

Can't you use the "deleteCard" message, which is sent just before the card is actually deleted, to update your list?

Craig

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Handling situations where a requested card doesn't exist

Post by glenn9 » Mon Nov 02, 2020 4:42 pm

Thank you all for your help.

Apologies for the lack of clarity in my question but have now managed to solve my difficulty.

I stumbled across the 'lineoffset' command in the dictionary which gave me the answer.

Just in case its of use to anyone, what I basically wanted to do was to delete a line of a list that contained some defined text.

so what I did was:
- put the text into a variable
- put the variable into the lineoffset command
- this told gave me the line number that the text was in
- and so I was then able to delete that line

The code looked like this:

Code: Select all

   if field"Field" of card "Main" contains tTextToDelete then
      
      go to card"Main"
      
      put lineoffset(tTextToDelete, field"Field") into tLinetoDelete
      
      delete line tLinetoDelete of field"Field"
I was particularly proud of myself as I'd not come across the lineoffset command before and managed to use it successfully!

Everyday a new livecode learning event... yesterday it was the 'exists' command!

Thank you again,

Glenn

jiml
Posts: 336
Joined: Sat Dec 09, 2006 1:27 am
Location: Los Angeles

Re: Handling situations where a requested card doesn't exist

Post by jiml » Tue Nov 03, 2020 8:26 pm

what I basically wanted to do was to delete a line of a list that contained some defined text.
You may find the FILTER command useful too.

Code: Select all

filter fld "myField" without "*tTextToDelete*"

glenn9
Posts: 223
Joined: Wed Jan 15, 2020 10:45 pm
Location: Europe

Re: Handling situations where a requested card doesn't exist

Post by glenn9 » Wed Nov 04, 2020 1:59 pm

jiml wrote:
Tue Nov 03, 2020 8:26 pm
what I basically wanted to do was to delete a line of a list that contained some defined text.
You may find the FILTER command useful too.

Code: Select all

filter fld "myField" without "*tTextToDelete*"
Thank you jiml, an elegant solution, another piece of learning for me.

Regards,

Glenn

Post Reply

Return to “Getting Started with LiveCode - Complete Beginners”