QB64 Phoenix Edition
Testing against multiple elements at a time without select case - Printable Version

+- QB64 Phoenix Edition (https://qb64phoenix.com/forum)
+-- Forum: QB64 Rising (https://qb64phoenix.com/forum/forumdisplay.php?fid=1)
+--- Forum: Code and Stuff (https://qb64phoenix.com/forum/forumdisplay.php?fid=3)
+---- Forum: Help Me! (https://qb64phoenix.com/forum/forumdisplay.php?fid=10)
+---- Thread: Testing against multiple elements at a time without select case (/showthread.php?tid=3619)



Testing against multiple elements at a time without select case - CMR - 04-16-2025

Select Case lets you test against things like this: Case 1, 2, 3, 4, 5  Is there a way to use this kind of expression list with 'If'?  I'm trying to make a little program that builds sprite sheets from a list of image files, and I'm testing to see if the file name extension matches an image files.  Here's the Sub I'm working on.  It just builds a list of file names from the files listed in a text file. 

Code: (Select All)

Sub LoadFromTxt( txt_file$, names_list$() )
    ''load a list of file names from a txt file
    Dim fp& : fp& = Freefile
    Dim tmp$
    Dim count& : count& = 1
   
    Open txt_file$, For Input as fp&
        While Not EOF(fp&)
            Line Input fp&, tmp$
            tmp$ = LTrim$(RTrim$(tmp$))
            If tmp$ <> "" Then
                ''test for image suffix .*
                Select Case Mid$(tmp$, Len(tmp$) - 4, 4)
                    Case ".png",".bmp",".jpg",".tga", _
                    ".psd",".gif",".hdr",".pic",".pnm", _
                    ".pcx",".svg",".ico",".cur",".qoi"
                        names_list$(count&) = tmp$
                        count& = count& + 1
                    Case Else
                        Print "Unknown file type " + tmp$
                End Select
            End If
        Wend
    Close fp&
End Sub

I just feel like it would look better with a couple of 'If' statements instead, and was wondering if you could use expression lists with If the way you could Select Case.


RE: Testing against multiple elements at a time without select case - SMcNeill - 04-16-2025

Something similar to this?

Code: (Select All)

Sub LoadFromTxt (txt_file$, names_list$())
''load a list of file names from a txt file
Dim fp&: fp& = FreeFile
Dim tmp$
Dim count&

Open txt_file$ For Input As fp&
While Not EOF(fp&)
Line Input #fp&, tmp$
tmp$ = Right$(_Trim$(tmp$), 4)
If Len(tmp$) = 4 Then
If InStr(".png.bmp.jpg.tga.psd.gif.hdr.pic.pnm.pcx.svg.ico.cur.qoi", tmp$) Then
count& = count& + 1
names_list$(count&) = tmp$
Else
Print "Unknown file type " + tmp$
End If
End If
Wend
Close #fp&
End Sub



RE: Testing against multiple elements at a time without select case - CMR - 04-16-2025

That's great.  I didn't even think of using InStr.  Thanks.


RE: Testing against multiple elements at a time without select case - eoredson - 04-16-2025

You could also try:

Code: (Select All)
Sub LoadFromTxt (txt_file$, names_list$())
  ' load a list of file names from a txt file
  Dim fp&: fp& = FreeFile
  Dim tmp$
  Dim count&: count& = 1

  Open txt_file$ For Input As fp&
  While Not EOF(fp&)
      Input fp&, tmp$
      tmp$ = LTrim$(RTrim$(tmp$))
      If tmp$ <> "" Then
        ' test for image suffix .*
        Restore
        Do
            Read Tst$
            If Tst$ = ".eod" Then Exit Do
            If Mid$(tmp$, Len(tmp$) - 4, 4) = Tst$ Then
              names_list$(count&) = tmp$
              count& = count& + 1
            End If
        Loop
      End If
  Wend
  Close fp&
End Sub
Data ".png",".bmp",".jpg",".tga",".psd",".gif",".hdr",".pic",".pnm",".pcx",".svg",".ico",".cur",".qoi",".eod"



RE: Testing against multiple elements at a time without select case - CMR - 04-17-2025

That's cool too.  Someday I need to do a deep dive into all the stuff you can do with DATA and READ statements.  Here's what I ultimately decided on.  It's very similar to SMcNeill's.  I decided to move the list count outside, so I can track my position in the array regardless of how many times I call this function or others.  I made the extension string its own thing also just to keep my line widths down.  Now I just need to test the thing and see if it works.   Big Grin

Code: (Select All)

Sub LoadFromTxt( txt_file$, names_list$(), list_count& )
    ''load a list of image file names from a txt file
    Dim fp& : fp& = Freefile
    Dim tmp$
   
    Dim f_ext$
    f_ext$ = ".png.bmp.jpg.tga.psd.gif.hdr.pic.pnm.pcx.svg.ico.cur.qoi"
   
    Open txt_file$, For Input as fp&
        While Not EOF(fp&)
            Line Input fp&, tmp$
            tmp$ = _Trim$(tmp$)
            If tmp$ <> "" Then
                ''test for image suffix .*
                If InStr(f_ext$, Right$(tmp$, 4)) Then
                        list_count& = list_count& + 1
                        names_list$(list_count&) = tmp$
                Else
                    Print "Unknown file type " + tmp$
                End If
            End If
        Wend
    Close fp&
   
End Sub ''LoadFromTxt