-1

I have a associative array as below

set USERS[johnh]=John Howard
set USERS[moeh]=Moe Howard
set USERS[larryk]=Lary King

When given johnh, it should select John Howard
When given moeh, it should give me Moe Howard

What kind of batch can do this job...

C B
  • 1,677
  • 6
  • 18
  • 20
goodywp
  • 21
  • 6
  • 2
    Come on a quick google search shows how arays work in batch. A simple `echo %USERS[johnh]%` displays the first array entry. The rest is just some input read and a few `if` – Paxz Aug 24 '18 at 19:59
  • Thanks- I tried to use echo %USER[%johnh%]% it gave me John Howard. Now my issue is that johnh can be a variable. So I tried to set user=johnh then either echo %USER[%user%]% or !USER[%user%]!, none working – goodywp Aug 24 '18 at 20:37
  • Follow @Compo's hint, use `Call Echo %%USERS[%user%]%%` – JosefZ Aug 24 '18 at 20:47
  • 1
    `!USER[%user%]!` should work. You remembered to enable delayed expansion first, right? Also, I have no idea how `%USER[%johnh%]%` worked for you; it shouldn't have. – SomethingDark Aug 24 '18 at 20:47
  • 1
    This topic is fully explained at [Arrays, linked lists and other data structures in cmd.exe (batch) script](https://stackoverflow.com/questions/10166386/arrays-linked-lists-and-other-data-structures-in-cmd-exe-batch-script/10167990#10167990)... – Aacini Aug 25 '18 at 02:44

1 Answers1

0

Other than using

Call Echo %%USERS[%~1]%%

or

Call Echo %%USERS[%user%]%%

as used in the comments, and despite your continued lack of effort, here's an example batch file:

@Echo Off
Set "USER[johnh]=John Howard"
Set "USER[moeh]=Moe Howard"
Set "USER[larryk]=Larry King"

:Askname
ClS
Set /P "Byname=Please enter your byname: "

Set "Fullname="
For /F "Tokens=1* Delims==" %%A In ('Set USER[ 2^>Nul'
) Do If /I "%%A"=="USER[%Byname%]" Set "Fullname=%%B"
If Not Defined Fullname GoTo Askname

Echo Hello %Fullname%

Pause

Note: I will not be extending my answer, or answering further questions based upon it.

Compo
  • 36,585
  • 5
  • 27
  • 39