Rehashing Switch
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Rehashing Switch
Richmond.
Hot dogs?
The only thing Americans enjoy that we are just a little sheepish about?
Craig
Hot dogs?
The only thing Americans enjoy that we are just a little sheepish about?
Craig
-
- Livecode Opensource Backer
- Posts: 9361
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Rehashing Switch
Oh; I didn't know they had mutton in them.Hot dogs?
The only thing Americans enjoy that we are just a little sheepish about?
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Rehashing Switch
Good thing I did not say "bullish about".
Craig
Craig
-
- Livecode Opensource Backer
- Posts: 9361
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Rehashing Switch
Quite.Good thing I did not say "bullish about".
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.
-
- VIP Livecode Opensource Backer
- Posts: 7227
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Rehashing Switch
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
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
HyperActive Software | http://www.hyperactivesw.com
-
- Livecode Opensource Backer
- Posts: 9361
- Joined: Fri Feb 19, 2010 10:17 am
- Location: Bulgaria
Re: Rehashing Switch
So, as I understand things, the 'problem' such as it is is because "we" all haveyou can do that with switch already
been thinking that between every CASE we HAVE to have a BREAK.
-
- VIP Livecode Opensource Backer
- Posts: 7227
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Rehashing Switch
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.richmond62 wrote: ↑Tue Mar 23, 2021 7:51 pmSo, as I understand things, the 'problem' such as it is is because "we" all haveyou can do that with switch already
been thinking that between every CASE we HAVE to have a BREAK.
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
HyperActive Software | http://www.hyperactivesw.com
-
- VIP Livecode Opensource Backer
- Posts: 7227
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Rehashing Switch
Here's potato sorting off the top of my head:richmond62 wrote: ↑Tue Mar 23, 2021 12:28 pmSo . . . 3 types of sweet potatoes; pink, yellow and brown have to be differentiated from 'spuds' that are pink, red, yellow and black.
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
HyperActive Software | http://www.hyperactivesw.com
Re: Rehashing Switch
Jacque, while you are keenly inserting breaks, note your last one is redundant, there being no further statements within the switch.
-
- VIP Livecode Opensource Backer
- Posts: 7227
- Joined: Sat Apr 08, 2006 8:31 pm
- Location: Minneapolis MN
- Contact:
Re: Rehashing Switch
So true. Just covering my bases.
Jacqueline Landman Gay | jacque at hyperactivesw dot com
HyperActive Software | http://www.hyperactivesw.com
HyperActive Software | http://www.hyperactivesw.com
Re: Rehashing Switch
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.
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: Rehashing Switch
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
Re: Rehashing Switch
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.
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.
-
- VIP Livecode Opensource Backer
- Posts: 9655
- Joined: Wed May 06, 2009 2:28 pm
- Location: New York, NY
Re: Rehashing Switch
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.
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.
-
- Posts: 720
- Joined: Thu Sep 11, 2014 1:49 pm
- Location: The Netherlands
Re: Rehashing Switch
Yep, me 2.
Go for it. I also vote for "continue". in Hindsight "continue" is much better than my suggestion...
Go for it. I also vote for "continue". in Hindsight "continue" is much better than my suggestion...