select key get paired value in batch
select key get paired value in batch
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...
Call Echo %%USERS[%~1]%%
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 at 19:59
echo %USERS[johnh]%
if
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 at 20:37
Follow @Compo's hint, use
Call Echo %%USERS[%user%]%%
– JosefZ
Aug 24 at 20:47
Call Echo %%USERS[%user%]%%
!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 at 20:47
!USER[%user%]!
%USER[%johnh%]%
1 Answer
1
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.
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
A batch file; and one you've written, or at least attempted to write yourself. StackOverflow isn't a free code writing service, here you supply the code for the intended task and we help you with fixing any specific replicable issue it has. You could try something like
Call Echo %%USERS[%~1]%%
.– Compo
Aug 24 at 19:56