Page 4 of 6

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 4:31 pm
by dunbarx
Richmond.

Hot dogs?

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

Craig

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 5:13 pm
by richmond62
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)

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 6:37 pm
by dunbarx
Good thing I did not say "bullish about".

Craig

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 7:13 pm
by richmond62
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 . . .

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 7:44 pm
by jacque
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.

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 7:51 pm
by richmond62
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.

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 7:54 pm
by jacque
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.

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 8:14 pm
by jacque
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

Re: Rehashing Switch

Posted: Tue Mar 23, 2021 11:40 pm
by Davidv
Jacque, while you are keenly inserting breaks, note your last one is redundant, there being no further statements within the switch. :)

Re: Rehashing Switch

Posted: Wed Mar 24, 2021 12:28 am
by jacque
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.

Re: Rehashing Switch

Posted: Wed Mar 24, 2021 12:42 am
by Davidv
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. :)

Re: Rehashing Switch

Posted: Wed Mar 24, 2021 9:09 am
by mrcoollion
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

Re: Rehashing Switch

Posted: Wed Mar 24, 2021 9:13 am
by Davidv
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.

Re: Rehashing Switch

Posted: Wed Mar 24, 2021 2:36 pm
by dunbarx
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.

Re: Rehashing Switch

Posted: Wed Mar 24, 2021 3:21 pm
by mrcoollion
Yep, me 2.
Go for it. I also vote for "continue". in Hindsight "continue" is much better than my suggestion...