Rehashing Switch

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

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

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

Re: Rehashing Switch

Post by dunbarx » Tue Mar 23, 2021 4:31 pm

Richmond.

Hot dogs?

The only thing Americans enjoy that we are just a little sheepish about?

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9249
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Rehashing Switch

Post by richmond62 » Tue Mar 23, 2021 5:13 pm

Hot dogs?

The only thing Americans enjoy that we are just a little sheepish about?
Oh; I didn't know they had mutton in them. 8)

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

Re: Rehashing Switch

Post by dunbarx » Tue Mar 23, 2021 6:37 pm

Good thing I did not say "bullish about".

Craig

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9249
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Rehashing Switch

Post by richmond62 » Tue Mar 23, 2021 7:13 pm

Good thing I did not say "bullish about".
Quite.

However, my shopping list was not meant as a sort of trite comment on what is under discussion.

The fact is that with SWITCH one could never end up with a shopping basket like that
indicated by the availability of goods on the shopping list . . .
Last edited by richmond62 on Tue Mar 23, 2021 7:49 pm, edited 1 time in total.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Rehashing Switch

Post by jacque » Tue Mar 23, 2021 7:44 pm

dunbarx wrote:
Tue Mar 23, 2021 2:47 pm
But sometimes I want to evaluate a suite of conditions, some true, some not, so that I can collect all the true ones and ignore the false ones. That is why I think enhancing "Switch" would be so useful.
Actually you can do that with switch already (including Richmond's shopping list, for that matter.) It's just a variation on what Thierry and I posted before. Collect all the "trues" and ignore all the rest (or vice versa.)

Code: Select all

function checkWithSwitch pVar
  switch
    case pVar = <conditionA>
    case pVar = <conditionB>
    case pVar = <conditionC>
      put pVar & cr after tTrueList
      break
    default
      put pVar & cr after tFalseList -- optional
  end switch
  return tTrueList -- or the false list
end checkWithSwitch
Edit: if you want to collect all the items, you'd call the above in a repeat loop, passing each item in pVar. Make tTrueList a script local variable.
Last edited by jacque on Tue Mar 23, 2021 7:56 pm, edited 2 times in total.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

richmond62
Livecode Opensource Backer
Livecode Opensource Backer
Posts: 9249
Joined: Fri Feb 19, 2010 10:17 am
Location: Bulgaria

Re: Rehashing Switch

Post by richmond62 » Tue Mar 23, 2021 7:51 pm

you can do that with switch already
So, as I understand things, the 'problem' such as it is is because "we" all have
been thinking that between every CASE we HAVE to have a BREAK.

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Rehashing Switch

Post by jacque » Tue Mar 23, 2021 7:54 pm

richmond62 wrote:
Tue Mar 23, 2021 7:51 pm
you can do that with switch already
So, as I understand things, the 'problem' such as it is is because "we" all have
been thinking that between every CASE we HAVE to have a BREAK.
Maybe.That's the beauty of switch; you can fall through any number of times and each case statement will evaluate the next until it hits a break. I use this all the time. It's a big advantage of switch over ifs.

On the other hand, you have to be careful because if one of the cases does need a break you have to be sure to insert it. I once reported a bug to Dr. Raney because my switch wasn't working right and he pointed out I'd forgotten to break. I was embarrassed.

Another edit: I forgot the break in my example. I just added it. Still embarrassed I guess.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Rehashing Switch

Post by jacque » Tue Mar 23, 2021 8:14 pm

richmond62 wrote:
Tue Mar 23, 2021 12:28 pm
So . . . 3 types of sweet potatoes; pink, yellow and brown have to be differentiated from 'spuds' that are pink, red, yellow and black.
Here's potato sorting off the top of my head:

Code: Select all

on sortPotatoes pType,pColor
  switch
    case pType = "spud" or pColor = "green"
      return -- we just skip this one
      break
      -- only sweet potatoes are left now:
    case pColor = "pink"
    case pColor = "yellow"
    case pColor = "brown"
      add 1 to potatoArray[pColor] -- presumably a script local
      break
  end switch
end sortPotatoes
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Rehashing Switch

Post by Davidv » Tue Mar 23, 2021 11:40 pm

Jacque, while you are keenly inserting breaks, note your last one is redundant, there being no further statements within the switch. :)

jacque
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 7210
Joined: Sat Apr 08, 2006 8:31 pm
Location: Minneapolis MN
Contact:

Re: Rehashing Switch

Post by jacque » Wed Mar 24, 2021 12:28 am

Davidv wrote:
Tue Mar 23, 2021 11:40 pm
Jacque, while you are keenly inserting breaks, note your last one is redundant, there being no further statements within the switch. :)
So true. Just covering my bases.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Rehashing Switch

Post by Davidv » Wed Mar 24, 2021 12:42 am

Sound, and you are more practised in LC than I am. When I notice something like that I am only trying to give fuller information to newer readers who may happen upon our code fragments. :)

mrcoollion
Posts: 709
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Rehashing Switch

Post by mrcoollion » Wed Mar 24, 2021 9:09 am

dunbarx wrote:
Sat Mar 20, 2021 7:44 pm
I want to enhance it, Perhaps a new keyWord that can substitute for "break', like "busted":
I am on the side of Craig because I have also tried to use the Switch statement the same way (it's a little bit easier to read). I also think it would be a nice enhancement. However, I would opt for the word Done instead of Busted.

Regards,

Paul

Davidv
Posts: 77
Joined: Sun Apr 09, 2006 1:51 am
Location: Australia

Re: Rehashing Switch

Post by Davidv » Wed Mar 24, 2021 9:13 am

So far we have (in order) busted, nobreak, continue, done.

Although having contributed nobreak, I now favour continue. So, why has nobody raised an enhancement request? I semi-volunteered earlier so I will do it tomorrow if the originator does not get in first.

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

Re: Rehashing Switch

Post by dunbarx » Wed Mar 24, 2021 2:36 pm

Go for it. I also vote for "continue".

To sum this all up, we all know that either control structure can be massaged to work in almost any situation. The choice of which one is best, or more comfortable, or more powerful, or easier to write and read, is more a matter of style than of explicit capability. As I said early, this is an enhancement, not a fix for something broken.

The originator.

mrcoollion
Posts: 709
Joined: Thu Sep 11, 2014 1:49 pm
Location: The Netherlands

Re: Rehashing Switch

Post by mrcoollion » Wed Mar 24, 2021 3:21 pm

Yep, me 2.
Go for it. I also vote for "continue". in Hindsight "continue" is much better than my suggestion...

Post Reply

Return to “Talking LiveCode”