Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Batch File - Get list value using index key

Tags:

batch-file

I have written a script to bat file that will print the random value from list. but I am facing problem to access list value using it's index

My code is:

   set list=A B C D a b c
   echo %list[3]%
   for /l %%a in (1,1,6) do (
    @set /a bottomlimit = 0
    @set /a upperlimit = 5
    @set /a num = !bottomlimit! + !RANDOM! %% !upperlimit! - !bottomlimit! + 1
    echo %list[!num!]%   
    TIMEOUT /T 5
   )

Waiting for your valuable solution.

like image 491
Anupam Sharma Avatar asked Aug 22 '26 04:08

Anupam Sharma


2 Answers

Just three options. Number one to handle your approach. Number two for a "pure" array in environment variables. Number three will mix the two, definition of the list as in option 1, but iterating the list to generate the array in option 2.

@echo off

    setlocal enableextensions enabledelayedexpansion

    REM OPTION 1 - The list
    echo -------------------------------------------------

    setlocal

    set "list=A B C D a b c"

    set /a bottomlimit=0
    set /a upperlimit=6

    for /l %%a in (1,1,6) do (
        set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
        set "pos=0"
        for %%l in (!list!) do if defined pos if !pos!==!num! ( echo %%l & set "pos=" ) else ( set /a "pos+=1")
    )   

    endlocal


    REM OPTION 2 - The "pure" array
    echo -------------------------------------------------

    setlocal 

    set "list[0]=A"
    set "list[1]=B"
    set "list[2]=C"
    set "list[3]=D"
    set "list[4]=a"
    set "list[5]=b"
    set "list[6]=c"

    set /a bottomlimit=0
    set /a upperlimit=6

    for /l %%a in (1,1,6) do (
        set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
        for %%n in (!num!) do echo !list[%%n]!
    )   

    endlocal



    REM OPTION 3 - The remix
    echo -------------------------------------------------

    setlocal

    set "list=A B C D a b c"

    set "pos=0"
    for %%l in (!list!) do ( set "list[!pos!]=%%l" & set /a "pos+=1" )

    set /a "bottomlimit=0"
    set /a "upperlimit=!pos!-1"

    for /l %%a in (1,1,6) do (
        set /a "num=!bottomlimit! + ( !RANDOM! %% (!upperlimit! - !bottomlimit! + 1))"
        for %%n in (!num!) do echo !list[%%n]!
    )   

    endlocal


    endlocal 
    exit /b
like image 198
MC ND Avatar answered Aug 27 '26 00:08

MC ND


@echo off
setlocal EnableDelayedExpansion
  set list=A B C D a b c
  set /a counter=1
   for %%a in (%list%) do (
    set "list[!counter!]=%%~a"
    set /a counter=counter+1
   )
   set list[


   for /l %%a in (1,1,6) do (
    @set /a bottomlimit = 0
    @set /a upperlimit = 5
    @set /a num = bottomlimit + !RANDOM! %% upperlimit - bottomlimit + 1
    for %%# in (!num!) do echo !list[%%#]!  
    TIMEOUT /T 5
   )
   endlocal
like image 32
npocmaka Avatar answered Aug 26 '26 23:08

npocmaka



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!