如何从环境变量字符串中删除多个重复的单词(语言名称)



我现在在运行 echo %subsnew%上有此输出:

English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German

如何删除变量subsnew中的重复项?

我需要输出:

English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish

这可以在只有2个语句的单个循环中完成。当前元素是从集合中删除的(这是棘手的部分,使用参数扩展从字符串中删除了变量子字符串);如果集合不更改,则尚未包含该元素,然后添加。

@echo off
REM input is a string with names
REM return a string with unique names
REM 5:00:06 PM Friday 24/11/2017
if not defined subsnew set "subsnew=English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German"
setlocal EnableDelayedExpansion
REM collection may not be empty
set "collection= "
for %%a in (%subsnew%) do (
    REM remove the current name from the collection
    set "test=!collection:%%a=!"
    REM if the collection has not changed, name was missing so it is added now
    if !test!==!collection! set "collection=!collection! %%a"
)
REM remove the leading 2 spaces
set collection=!collection:~2!
echo final string is ^>%collection%^<

这与MOFI提供的想法相似,除了应该更快一点,并且应保持唯一变量值的顺序。

@Echo Off
Set "subsnew=English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German"
SetLocal EnableDelayedExpansion
Set "tmpExt=.$%RANDOM%"
Set "tmpVar="
For %%A In (%subsnew%) Do If Not Exist "%%A%tmpExt%" (Break>"%%A%tmpExt%"
    Set "tmpVar=!tmpVar! %%A")
Del/Q "*%tmpExt%"
EndLocal&Set "subsnew=%tmpVar:~1%
Echo %subsnew%
Pause

前两行和最后两行是出于插图目的,只有那些需要粘贴到现有脚本中的线。

此任务评论了批处理代码:

@echo off
rem Define the languages for testing removing duplicates if not already defined.
if not defined set "subsnew=English English Arabic Danish Dutch Finnish French German Hindi Norwegian Swedish Turkish English Dutch French German English French German"
rem Create a local environment for removing the duplicates with enabled
rem command extensions needed for command FOR /F and for command SET with
rem using syntax "variable=value" and enabled delayed environment variable
rem expansion required for rebuilding the language list without duplicates.
setlocal EnableExtensions EnableDelayedExpansion
rem Create for each language an environment variable starting with LANG_
rem and name of the language with value 1. The value is not important.
for %%I in (%subsnew%) do set "LANG_%%I=1"
rem Delete the environment variable subsnew.
set "subsnew="
rem Run command SET with argument LANG_ to get listed alphabetically sorted
rem all environment variables starting with LANG_ line by line in format
rem variable=value, e.g. LANG_English=1, LANG_Finnish=1, ...
rem Command FOR executes the command in a separate command process started
rem with cmd.exe /C in background and captures all lines output to handle
rem STDOUT for processing them next line by line.
rem Each line is split up into substrings using underscore and equal sign
rem as string delimiters. The first string is always LANG which is of no
rem interest. The third string is always 1 which is the value of each
rem LANG_* environment variable after the equal sign which is also of no
rem interest. Therefore only the second string being the language string
rem is processed further by assigning it to loop variable I.
rem The environment variable subsnew is rebuild with the language strings.
rem Delayed environment variable expansion is needed as the help output on
rem running in a command prompt window the command SET /? explains on a very
rem similar example to the command line below.
for /F "tokens=2 delims=_=" %%I in ('set LANG_') do set "subsnew=!subsnew! %%I"
rem Restore previous environment which means discarding all environment
rem variables defined and modified after command SETLOCAL above.
rem But the new value of environment variable subsnew is needed in previous
rem environment. So subsnew must be defined again in previous environment
rem with the current value of subsnew in current environment without the
rem space at begin which is done with the command line below.
endlocal & set "subsnew=%subsnew:~1%"
rem Output value of environment variable subsnew with the languages
rem sorted alphabetically and duplicate language strings removed.
echo %subsnew%
pause

请注意,此方法仅适用于不包含语言字符串本身中空间字符的语言字符串。但这看起来没问题。

用于了解使用的命令及其工作方式,打开命令提示符窗口,执行以下命令,并完全仔细阅读所有命令显示的所有帮助页面。

  • echo /?
  • endlocal /?
  • for /?
  • pause /?
  • rem /?
  • set /?
  • setlocal /?

最新更新