!AppBasic App Fails to Work Correctly
Richard Ashbery (495) 163 posts |
I have a simple !AppBasic program that searches a group of numbers in turn (each of which is separated with a comma) and displays the highest found. The numbers are typed into a ‘Writable field’ gadget (0-9, – and , are permitted). A display gadget displays the numbers as they are typed. The event-handler is as follows: DEF PROCDealWith_Window(event,object,component) The program works for positive numbers but if you first type a ‘-’ character a 0 is displayed. Typing -1 displays correctly but as soon as a comma is typed display returns 0. I’m sure there is a simple solution to overcoming this problem but I can’t see it. Any ideas welcome. |
Steve Fryatt (216) 2046 posts |
You need to consider what the code in Your quoting was broken, but I’m assuming that the definition is
The procedure starts with Next, you type ‘-1’. The next problem comes from your While your approach is valid and could be made to work as intended, there’s a lot of benefit in breaking a problem down into smaller chunks – especially if those chunks are more generally useful. Processing comma-separated lists of data is a universal problem, so let’s start by creating a function which will take a comma-separated string, pull the first field off the front and return it, and then update the supplied string so that the field has been removed.
Armed with this, we can then create a more robust “find maximum” function which is more forgiving of missing fields.
And then finally, you can use this in
It’s more verbose, but it’s also clearer (to me, at least). Because each bit does only a small part of the problem, things can be tested and checked and made to return sensible values when the input isn’t what’s expected. This is important when dealing with external input. There’s still the problem that fields which don’t parse as numbers will evaluate to zero, but to be honest I’m not sure what else you’d want to evaluate them to. Unfortunately BASIC can’t be made to throw an error when |
Steve Drain (222) 1620 posts |
Whilst I totally agree with Steve about breaking things down into manageable chunks, I do see a way of using This modified procedure could do what you want, but there might be a glitch I have not seen. ;-)
Edit: Only enter the local error line once you have all the variables in the repeat structure correctly entered. |
Richard Ashbery (495) 163 posts |
Thanks for your replies Steve and Steve. I’ll go through your analysis/recommendations in detail and get back to you if I still have problems. I’m concerned that I still have issues with quoting. |
Chris (121) 470 posts |
Textile can be a bit tricky :) To quote multi-line code, put the bc.. REM Window Same with the If you’re more used to HTML tags, then using |
Steve Drain (222) 1620 posts |
There is, if the first field is invalid. Changing to a
|
Richard Ashbery (495) 163 posts |
Re: Steve D As you correctly pointed out there is a glitch in your first example – crashes when “-” or “,” is typed first. However the second “WHILE… ENDWHILE” example improves things by maintaining the highest number found until a higher one is typed. The only minor issue is that if a “-” or “,” as either of the first characters I get a 0 returned. When say -5 is typed -5 will be displayed correctly. A following comma is not displayed which is exactly what I’m looking for. Re: Steve F Code I submitted – what an excellent description as to how it works – confirms my understanding. Running your code as ‘stand-alone’ works a treat – I need to spend time trying to understand how it works. If I integrate it into !AppBasic I get exactly the same issue as with Steve D’s second “WHILE… ENDWHILE” example. |
Steve Fryatt (216) 2046 posts |
I assume that’s the fact that if the first character isn’t a valid number, you don’t get the expected result? Thinking about it, the initial exit of my version wasn’t quite correct: it would ignore the whole of the rest of the list if the first item was empty (ie. if the string started with a comma, such as “,4,3,2”). This version is probably closer to what you’re after:
I’m still struggling to understand the issue about “-” as the leading character; I’m assuming that you mean something like “-,-1”? If so, the problem is still that we’re trying to set maximum using
The Armed with this, we can now re-write
This version starts by jumping ship if it’s passed an empty string. Otherwise, it starts pulling fields off the front of the string and passing any which aren’t empty to We then drop into the main loop as before, but now we again only consider values for comparison with Clearly if there are no valid fields, |
Steve Drain (222) 1620 posts |
This has clearly grabbed our joint attention, and that [above] is indeed the problem. Here is my way to use
|
Richard Ashbery (495) 163 posts |
Steve F: I’m still trying to work through your last code example but unfortunately getting freezes – not given up yet (sheer hell and bedlam at our place due to decorators – so even poorer concentration than usual). I’ve implemented Steve D’s last posting – I’m sure you’ll be pleased, Steve – it works superbly with the first field being interrogated correctly for valid number. Just another thank you to you both for your explanations and clever coding techniques. Next project – detecting duplicate words in a phrase – should be easy enough!! |
Steve Drain (222) 1620 posts |
“I love it when a plan comes together.” However, I cannot stop tinkering, so here is the same thing, but briefer:
That is my test code in my own style, but it should be easy to convert. Here are some thoughts:
Despite my efforts, Basalt cannot improve on using Note that Because the string is limited to a tight set of characters, |
Steve Drain (222) 1620 posts |
Do you mean things like like this? |
Steve Pampling (1551) 7932 posts |
Next project – detecting duplicate words in a phrase It should be noted that there are instances that that does not trigger. |
Richard Ashbery (495) 163 posts |
I was thinking in terms of repetition of words in poignant phrases such as Winston Churhill’s famous war speech… “We shall fight on the beaches, we shall fight on the landing grounds, we shall fight in the fields and in the streets, we shall fight in the hills, we shall never surrender.” Apart from words of less than 3 letters, shall is repeated 5 times and fight 4 times. I blame this on my Grandson – he’s a 12 year old who’s taken a keen interest in programming albeit Python and a host of other largely scientific interests – way beyond me :-(( I’m constantly having to come up with project ideas. Ref: Your latest brief version of highest number program with valid number detection… I’ll go through it and see if I can convert it for !AppBasic. Thanks again for posting. |
Rick Murray (539) 13405 posts |
“Brexit means Brexit”? Or too short? With reference to your better quote, I think there is a psychological impact that basic analysis would miss. It isn’t a lot of “shall” and a lot of “fight”, it is intentionally repeating the “we shall fight” (entire phrase). The impact of the quote is more or less the same if you remove all the other words and just repeat that phrase four times followed by the never surrender part. Although, of course, that shorter version sounds like a Donald quote. :-/ |
Chris Evans (457) 1614 posts |
With more and more and yet more padding. I think it is more difficult to spot when the duplicate word means that that the word duplication is on two different lines. This demonstration will only work if your browser wraps this post at the same point as mine. I know proof readers do things like search for " and and " but " that that " can be valid. I recall reading that it is possible to contrive a valid sentence that has " that that that that that " in it. IIRC someone is talking about the writing of a sign that has the word “that” in it or may be it was " that that " in it! |
Clive Semmens (2335) 3130 posts |
John, where James had had “had had,” had had “had had had.” Had “had had had” not been an error, it would have been quite impressive. Chris, where Clive had had ‘had had “had had,” had had “had had had.” Had “had had had”…’ |
Richard Ashbery (495) 163 posts |
No – that’s a good one. Perhaps it is too short. Ref: Churchill’s famous speech. I take your point Rick about “we shall fight” but code would have to search for “shall fight” as “we” is discounted (< 3 letters) which still makes it a tad tricky because of the space. As you are aware the space (in a similar way to the comma) is used as a delimiter in order to recognise individual words. Maybe this is not the best example. |
Clive Semmens (2335) 3130 posts |
The logic of using a delimiter and then looking at not only delimited items but also consecutive pairs of delimited items isn’t that difficult though. There might even be examples where you wanted to look at triplets, or even (horror of horrors) pairs separated by one (or more…) random items. |