Assistance using Arrays

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
viroid
Posts: 8
Joined: 04 Jan 2010 15:49

Assistance using Arrays

#1 Post by viroid » 17 Jan 2010 18:28

Hey guys, I'm trying to figure out how to use 'arrays' and I'm having some trouble. I wasn't able to get the :getipconfig function to work under Windows 7; I was using some sample code from another post that pulled .IPAddress from the array

call:getIPConfig arr
echo.%arr[1].IPv4Address%
echo.%arr[2].IPv4Address%
echo.%arr[3].IPv4Address%

I think in vista though the IP Address line actually reads IPv4 and IPv6 so the sample code to echo the IPAddress may not have been correct. I tried changing it to IPv4Address and that didn't seem to work either.

I also found the :l2a and :dumparr functions and attempted to get those working. I created a text file (list.txt) in the same folder as the batch file and passed it to :l2a using call:l2a list.txt test and then used call:dumparr test in an attempt to output the data, no luck. I'm just missing something and it's not making sense right now.


I'm attempting to store variables in an array and obviously query them as needed, but I also need to be able to update specific variables as well.

Array (at least, in my head) looks something like this:

THREADS 1 2 3 4
RUNNING yes yes no no
OVERFIVEMIN yes no no no
TIMESTARTED 23000000 23040000 0 0
PID 1823 1938 0 0

So basically, If the number of threads overfivemin is equal to the number of threads running, then start an additional thread (in this case, up to 4), when each thread is started I'll need to set the RUNNING, PID, and TIMESTARTED variables for that thread. Later I'll query each PID and when a process ends I'll reset the variables for that thread, then loop back through everything.


Is an array just a bunch of numbered variables, or does it function a little differently? I probably could just set 20 different variables to get this done, but where is the fun in that. If someone could explain arrays, and how the data is stored and retreived, that would be great.


...v

avery_larry
Expert
Posts: 391
Joined: 19 Mar 2009 08:47
Location: Iowa

#2 Post by avery_larry » 18 Jan 2010 13:19

There is no such thing as an array in DOS. Anything you do will be an approximation of the idea that is an array.

I use numbered variables, which you can combine with for loops to manipulate in interesting ways.

What I would at least start off doing:

numthreads = total number of threads. Since they appear to always be sequential, you only need 1 variable telling how many there are. In your example, the value would be 4.
running1=yes
running2=yes
running3=no
running4=no
overfivemin1=yes

you get the idea.

But now you can use a for loop:

Code: Select all

for /l %%a in (1,1,%numthreads%) do (
   echo Thread #%%a
   call echo Is it running?  %%running%%a%%
   call echo Has it been 5 minutes?  %%overfivemin%%a%%
   call echo It started at %%timestarted%%a%% with PID %%PID%%a%%
)


What you're doing also approximates an array. You'll have to pull out each value using a for loop (I believe):

Code: Select all

for /f "tokens=2" %%a in ("%thread%") do echo Thread #%%a
for /f "tokens=2" %%a in ("%running%") do echo Is it running?  %%a

Post Reply