Skip to content

How to Implement a Two-Dimensional Array in Batch Script

Learn how to create and traverse a 2D array in Windows Batch scripting using joined variable names (e.g., MAP_Row_Col) for tabular data like CSV grids or coordinate systems.

Standard arrays are "Linear" (a single list), but much of the data we handle is "Tabular", organized into rows and columns. Think of a CSV file, a grid of server resources, or a coordinate system. A Two-Dimensional (2D) Array allows you to store data using two coordinates (e.g., Grid[Row][Column]). While Batch doesn't have a native matrix object, we can simulate one using joined variable names.

In this guide, we will demonstrate how to build and traverse a 2D data structure.

The Strategy: Joined Indexes

The secret to a 2D array in Batch is a variable naming convention like MAP_Row_Col.

  • To store data at Row 2, Column 3: set "MAP_2_3=DataValue".
  • To retrieve it: use the call or for expansion trick to resolve both indices dynamically.

Implementation Script: 3x3 Multiplication Table

@echo off
setlocal enabledelayedexpansion

:: 1. Define grid dimensions
set "rows=3"
set "cols=3"

:: 2. Initialize the 2D Array
:: Fill a 3x3 grid with multiplication results
for /L %%R in (1,1,%rows%) do (
    for /L %%C in (1,1,%cols%) do (
        set /a "TABLE_%%R_%%C=%%R * %%C"
    )
)

:: 3. Accessing a Single Coordinate
set "r=2"
set "c=3"
call set "cellValue=%%TABLE_!r!_!c!%%"
echo Value at Row !r!, Col !c! is: !cellValue!
echo.

:: 4. Reading the Entire Grid (Traversal)
echo --- FULL %rows%x%cols% GRID ---
for /L %%R in (1,1,%rows%) do (
    set "rowStr="
    for /L %%C in (1,1,%cols%) do (
        set "rowStr=!rowStr! !TABLE_%%R_%%C!"
    )
    echo !rowStr!
)

endlocal
pause

Why Use a 2D Array in Batch?

  1. Tabular Processing: Directly modeling a CSV or database table in memory where you need to access specific "Cells" by their row and column numbers.
  2. Mapping Systems: Storing a list of "Servers" (Rows) and their "Services" (Columns), allowing you to check the status of a specific service on a specific server.
  3. Complex Logic: Building interactive "Choice Menus" where the options change based on the current "Category" and "User Level."

Important Considerations

  1. Memory Usage: A 100×100 grid creates 10,000 separate variables. This can quickly fill up the environment variable memory. Use 2D arrays for smaller, structured datasets.
  2. Index Delimiters: Use an underscore (e.g., _) between your coordinates. If you use GRID%R%%C%, index 1 and 11 would both become GRID11, causing a data collision.
  3. Traversal Complexity: Reading a 2D array requires nested loops. Ensure your loop bounds are correctly calculated to avoid "variable not defined" errors.

Best Practices

  1. Namespacing: Always use a unique prefix (like TABLE_ or GRID_) to prevent your coordinates from accidentally conflicting with other system or script variables.
  2. Store Dimensions: Keep your row and column counts in variables (rows, cols) so that all loops reference them consistently and the grid can be resized by changing two values.
  3. Documentation: Comment on which index represents the Row and which represents the Column, as it is easy to swap them accidentally during complex math operations.

Conclusion

Implementing the concept of a two-dimensional array adds a layer of spatial organization to your scripts. By moving beyond simple lists and utilizing coordinate-based storage, you can model complex datasets like tables, grids, and matrices with absolute precision. This architectural approach is essential for building professional reporting tools, inventory managers, and data-visualization scripts that require structured, multi-axis information management.

Navigation