Search code examples
windowsbatch-file

Passing a parameter with a space to a Perl program in a Window's batch file


I have a following batch file:

@echo off
set GENDER_LIST=M F
set AGE_GROUP_LIST="[20, 30)" "[30, 40)"

for %%G in (%GENDER_LIST%)    do (
    for %%A in (%AGE_GROUP_LIST%) do (
        perl argvSample.pl "%%G %%A"
        echo ---------------
    )
)

and a following Perl program:

use strict;

print "\"".join("\", \"", @ARGV)."\"\n";

Running the batch results in the following unexpected output:

"M [20,", "30)"
---------------
"M [30,", "40)"
---------------
"F [20,", "30)"
---------------
"F [30,", "40)"
---------------

I want it to output the following:

"M [20, 30)"
---------------
"M [30, 40)"
---------------
"F [20, 30)"
---------------
"F [30, 40)"
---------------

How should I fix the batch file to get the desired output?


Solution

  • "%%G %%A"
    

    produces

    "F "[30, 40)""
    

    Replace it with

    "%%~G %%~A"
    

    See output of help for.