How to iterate through IncludeBinary like an array

Just starting out? Need help? Post your questions and find answers here.
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

How to iterate through IncludeBinary like an array

Post by OgreVorbis »

I have a bunch of numbered files that I want to put in the DataSection with IncludeBinary. They need to be numbered, I'm not just being lazy :)
The IncludeBinary part is not too annoying, but what is annoying is that I have to CatchImage many many times. If they were stored in files, I could just use a For loop, but I can't figure how to do that with reading the IncludeBinary.

It uses a pointer like: CatchImage(0, ?Digit1)
etc...
Maybe something like this:

Code: Select all

For i = 0 To 10
   CatchImage(i, ?Digit + i)
Next
Maybe there is a smarter way? Or maybe a Macro could be used (that's less pure though)?

It is possible for me to do it manually, but it will add a ton of lines to my program and look junky.
My blog/software site: http://dosaidsoft.com/
User avatar
mk-soft
Always Here
Always Here
Posts: 5335
Joined: Fri May 12, 2006 6:51 pm
Location: Germany

Re: How to iterate through IncludeBinary like an array

Post by mk-soft »

Only adjust with IncludeBinary and CatchImage ...


Unfortunately, the vtImages no longer works under C-Backend.

Code: Select all

Structure ArrayOfPointer
  *pvData[0]
EndStructure

*Data.ArrayOfPointer = ?vtImages
For i = 0 To 4
  Debug PeekI(*Data\pvData[i])
Next

DataSection
  vtImages:
  Data.i ?Image0
  Data.i ?Image1
  Data.i ?Image2
  Data.i ?Image3
  Data.i ?Image4
  
  Image0:
  Data.i 000
  
  Image1:
  Data.i 100
  
  Image2:
  Data.i 200
  
  Image3:
  Data.i 300
  
  Image4:
  Data.i 400
  
EndDataSection
My Projects ThreadToGUI / OOP-BaseClass / EventDesigner V3
PB v3.30 / v5.75 - OS Mac Mini OSX 10.xx - VM Window Pro / Linux Ubuntu
Downloads on my Webspace / OneDrive
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to iterate through IncludeBinary like an array

Post by RASHAD »

Maybe

Code: Select all

Restore imageBuffer

For img = 0 To 3
  Read.i imageBuffer
  CatchImage(img,imageBuffer)
Next

DataSection    
  Image0:
    IncludeBinary "d:\Test.tif"
    
  Image1:
    IncludeBinary "d:\Test.tif"    
  
  Image2:
    IncludeBinary "d:\Test.tif"
    
  Image3:
    IncludeBinary "d:\Test.tif"
    
  imageBuffer:
    Data.i ?Image0,?Image1,?Image2,?Image3
  
EndDataSection
Egypt my love
OgreVorbis
User
User
Posts: 77
Joined: Thu Jan 16, 2020 10:47 pm

Re: How to iterate through IncludeBinary like an array

Post by OgreVorbis »

RASHAD wrote: Sat Nov 20, 2021 1:39 am Maybe
That works! Thanks :)

That's what I'm going to use, but I wonder if a Macro can put together variable names before execution? Like in my example where I try to concatenate
?Digit + i
Seeing as macros run at compile time, I would think they could construct a variable this way, but I'm not sure if the functionality exists. I don't need it, but I'm just curious.
My blog/software site: http://dosaidsoft.com/
RASHAD
PureBasic Expert
PureBasic Expert
Posts: 4636
Joined: Sun Apr 12, 2009 6:27 am

Re: How to iterate through IncludeBinary like an array

Post by RASHAD »

You are welcome :)
Egypt my love
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How to iterate through IncludeBinary like an array

Post by #NULL »

OgreVorbis wrote: Sat Nov 20, 2021 8:07 pmThat's what I'm going to use, but I wonder if a Macro can put together variable names before execution?
That's how I understood your original question and I tried with recursive Macro calls and CompilerIf and MacroExpandedCount but no luck so far.
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How to iterate through IncludeBinary like an array

Post by #NULL »

But you can just create a pb file with the file commands and include that file then

Code: Select all

For i = 0 To 10
  WriteStringN("CatchImage(#img_" + i + ", ...)
#NULL
Addict
Addict
Posts: 1440
Joined: Thu Aug 30, 2007 11:54 pm
Location: right here

Re: How to iterate through IncludeBinary like an array

Post by #NULL »

You can also use MacroExpandedCount so you don't have to write out manually all the numbers and can use duplicate lines

Code: Select all

Macro cnt
  MacroExpandedCount
EndMacro

CatchImage(cnt, 123)
CatchImage(cnt, 123)
CatchImage(cnt, 123)
Post Reply