SELECT CASES ???
#11
(10-16-2022, 10:28 PM)Pete Wrote:
(10-16-2022, 09:57 PM)SMcNeill Wrote: If it's a FUNCTION, then doesn't it need a return variable associated with it?

x = SatzLesen (or such)

That's the first thing you've been right about in the last 10 minutes. Either that or something like...
Code: (Select All)
PRINT SatzLesen

Pete

Unfortunately that didn't work at all.

There's the database, and I've changed the "Sub" to a function so that it returns the specified record number. The corresponding motorcycle should then be shown for each set number.
I couldn't find anything on the net about a function in a Select ... Case query. If it works with a sub, it must also work with a function, right? What kind of error is there just a complaint?

Code: (Select All)
$Console:Only

Option _Explicit

'Definition der Datenstruktur - Direktzugriff
Type MotorradModell
  Modell As String * 20
  Farbe As String * 10
  Hubraum As String * 10
  Kilowatt As String * 10
  Fahrgewicht As String * 10
  Preis As Double
End Type

'Global zur Verfuegung stellen, sonst wird es
'wirklich kompliziert
Dim Shared Motorrad As MotorradModell

Declare Sub Eingabe()
Declare Sub Lesen()
Declare Function SatzLesen(0)

Dim As Integer auswahl

Nochmal:
Cls
auswahl = 0
Locate 3, 4
Print "Waehlen Sie das gewuenschte Programm."
Locate 6, 10
Print "In Datei schreiben    -> 1"
Locate 7, 10
Print "Datei lesen           -> 2"
Locate 8, 10
Print "Bestimmten Satz lesen -> 3"
Locate 9, 10
Print "Programm beenden      -> 4"

Locate 11, 4
Input "Ihre Wahl bitte: ", auswahl
Print

Select Case auswahl
  Case 1
    Call Eingabe
  Case 2
    Call Lesen
  Case 3
    SatzLesen
  Case 4
    End
  Case Else
    Beep: Locate 12, 4
    Print "Falsche Eingabe!"
    Sleep 1
    GoTo Nochmal
End Select

End 'Hauptprogramm

'Neue Datei erstellen und Daten einlesen
Sub Eingabe

  Dim As Integer SatzNummer
  Dim As String Antwort

  Open "Motorrad.Dat" For Random As #1 Len = Len(Motorrad)

  SatzNummer = LOF(1) \ Len(Motorrad)

  'Neue Datensaetze hinzufuegen
  Do
    Input "Modell     : ", Motorrad.Modell
    Input "Farbe      : ", Motorrad.Farbe
    Input "Hubraum    : ", Motorrad.Hubraum
    Input "Kilowatt   : ", Motorrad.Kilowatt
    Input "Fahrgewicht: ", Motorrad.Fahrgewicht
    Input "Preis      : ", Motorrad.Preis

    SatzNummer = SatzNummer + 1

    'Datensatz in Datei schreiben
    Put #1, SatzNummer, Motorrad

    'Sollen weitere Daten eingegeben werden?
    Input "Weiter J/N: ", Antwort$
  Loop Until UCase$(Antwort$) = "N"

  Close 1#
End Sub

'Datensaetze sequentiell auslesen (alle)
Sub Lesen

  Dim As Integer AnzahlSaetze, SatzNummer

  Open "Motorrad.Dat" For Random As #1 Len = Len(Motorrad)

  'Anzahl der Datensaetze berechnen
  AnzahlSaetze = LOF(1) \ Len(Motorrad)

  'Datensaetze lesen und anzeigen
  For SatzNummer = 1 To AnzahlSaetze
    Get #1, SatzNummer, Motorrad

    'Daten anzeigen
    Print Tab(4); "Modell     : ", Motorrad.Modell
    Print Tab(4); "Farbe      : ", Motorrad.Farbe
    Print Tab(4); "Hubraum    : ", Motorrad.Hubraum
    Print Tab(4); "Kilowatt   : ", Motorrad.Kilowatt
    Print Tab(4); "Fahrgewicht: ", Motorrad.Fahrgewicht
    Print Tab(4); Using "Preis      : #####.##"; Motorrad.Preis
    Print
    Print Tab(4); "---------------------------------"
    Print
  Next

  Close 1#
End Sub

Function SatzLesen ()

  Const Falsch = 0, Wahr = Not Falsch
  Dim As Integer AnzahlSaetze, BestimmterSatz, SatzNummer

  Open "Motorrad.Dat" For Random As #1 Len = Len(Motorrad)

  'Anzahl der Datensaetze berechnen
  AnzahlSaetze = LOF(1) \ Len(Motorrad)
  BestimmterSatz = Wahr

  Do
    Print
    Print Tab(4); "Satznummer: ";
    Print "(Null zum Beenden): ";
    Input " ", SatzNummer

    'Warum "AnzahlSaetze + 1"? War intuitiv!
    If SatzNummer > 0 And SatzNummer < AnzahlSaetze + 1 Then
      Get #1, SatzNummer, Motorrad

      'Bestimmten Datenssatz anzeigen
      Print
      Print Tab(4); "Modell     : ", Motorrad.Modell
      Print Tab(4); "Farbe      : ", Motorrad.Farbe
      Print Tab(4); "Hubraum    : ", Motorrad.Hubraum
      Print Tab(4); "Kilowatt   : ", Motorrad.Kilowatt
      Print Tab(4); "Fahrgewicht: ", Motorrad.Fahrgewicht
      Print Tab(4); Using "Preis      : #####.##"; Motorrad.Preis
    ElseIf SatzNummer = 0 Then
      AnzahlSaetze = Falsch
    Else
      Print: Print: Beep: Print Tab(4); "Satznummer ausserhalb des Bereichs!"
    End If
  Loop While BestimmterSatz = 0
  SatzLesen = SatzNummer
End Function
Reply
#12
YIKES!
Ask me about Windows API and maybe some Linux stuff
Reply
#13
The point being you can't just call a function like you do a sub. Frankly, I'd just change the function to a sub, and define SatzLesen or drop it and just pass back SatzNummer.

The PRINT SatzLesen wasn't meant s a solution, it was just an example of using a function in QB64 without anything being returned. In general, functions either must return a value or have an active part in a statement like foo = SatzLesen or foo = Steve \ SatzLesen, etc.

So for subs, just call them. For functions, have something returned involved in the call statement, and not just the call alone.

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#14
I'd try removing these three lines as they're not needed:

Code: (Select All)
Declare Sub Eingabe()
Declare Sub Lesen()
Declare Function SatzLesen(0)

Honestly, that last line looks wrong to me. I don't think that 0 needs to be in there.
Reply
#15
DECLARE statements are just ignored in QB64. Left over QBasic stuff.

Pete
Reply
#16
You can declare a function and sub with the same name
Ask me about Windows API and maybe some Linux stuff
Reply
#17
(10-16-2022, 09:23 PM)SMcNeill Wrote:
(10-16-2022, 09:12 PM)Pete Wrote: Two votes for cool, 1 vote yuck. Cool wins. When can we expect the roll out??? Big Grin

Pete

I dunno.  How quick can you push a pull request with the changes you've implemented for this into the repo, so we can reject them?  Big Grin

I imagine the roll out would probably be two days after never, after that!

Tongue

Kidding aside, I honestly don't think any developer would waste their time ever typing to adopt such a syntax into the language.  

So are you saying...

(10-11-2022, 03:38 PM)SMcNeill Wrote: "Hey, I personally don't need X feature, so it's a waste to include it in the language!"
Reply
#18
@dbox Aye, that's my opinion, just as other folks find other features and additions to be a waste.  Everyone is entitled to their opinion, and, as an open source project, everyone is free to work on the project and develop new syntax and capabilities.  As I mentioned, I don't think any of the existing developers are going to implement such a syntax into the language for three main reasons:

1) This syntax can already be accounted for, almost letter by letter, simply by using existing IF THEN statements.
2) The proposed syntax is both wordier and more inefficient than the existing method.
3) The developers we have are all volunteers who work to *improve* the language in their spare time.  This change doesn't improve anything.



But, with that said, I'd encourage Pete -- or whomever wants to, including you -- to work up the changes yourself and push them into the repo.  We always try and support new developers, and this really doesn't seem like it'd be overly complicated for someone to use as a break-in exercise to learning and interacting with the qb64pe.bas source.

Just because none of the current devs would be interested in spending their precious free time to add or make a suggested alteration, that doesn't mean you can't make it yourself.  *That's* the beauty of open-source programming.  Wink
Reply
#19
Personally I do actually like this kind of syntax, C# has similar functionality via doing a
switch()
(equivalent of
SELECT CASE
) on a 'tuple' value (which is basically just a variable that holds multiple other variables). You can then have cases like
case (> 0, < 20)
where the first and second values in the tuple have to match those conditions. I've used it a few times and it makes it very easy to read the actual conditions you're checking for vs. having nested
switch()
or a bunch of
if
statements.

I will say that it has some downsides. It's a nice syntax in terms of being easy to read, but in my experience it makes it a bit harder to verify that all possible cases are actually covered (since the possible combinations quickly balloons).

Additionally IMO performance isn't a huge concern. A "good" compiler would be capable of optimizing this statement such that it doesn't matter how you write it. QB64 is not there, so certainly there's a potential performance penalty due to the likely multiple comparisons used to implement it, but IMO not enough to really matter anyway for most people. That is also something that could potentially be improved in the future sometime after the command is added.

That said, echoing what Steve said a bit, I can't pretend I have enough time to look at implementing something like this, I have too much I'm trying to get done already XD If someone was interested though I'd encourage them to submit a proposal on GitHub and we could talk about potential syntax and such (I don't think the syntax Pete gave would quite work).
Reply
#20
It's been what, 6 or 7 years since I wrote a key input routine in C. I vaguely remember using the SWITCH statement in that routine. I agree that a good compiler could optimize the IF/THEN statements, but I would get a bit concerned at what point relationship comparisons are allowed. Two elements, as my y and x example, are not too difficult but what if someone wanted u, v, x, y, z all in one CASE relationship? That gets to be a hell of a lot of nesting.

Just for the y, x example, I imagine a C translation would look something like this...

Code: (Select All)
if(y > 0)
{
if(x > 0)
{
foo(do something);
}
if(x = 0)
{
foo(do something);
}
if(x < 0)
{
foo(do something);
}
}
else if(y = 0)
{
if(x > 0)
{
foo(do something);
}
if(x = 0)
{
foo(do something);
}
if(x < 0)
{
foo(do something);
}
}
else if(y < 0)
{
if(x > 0)
{
foo(do something);
}
if(x = 0)
{
foo(do something);
}
if(x < 0)
{
foo(do something);
}
}

I don't have an editor, and bracket languages drive me a bit buggy, but I would think this is close to what would be needed to work out a template for conversion.

Pete
Reply




Users browsing this thread: 16 Guest(s)