Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

separating variable with a symbol into different parts

using batch, i want to be able to separate a variable into two or three parts, when there is a symbol dividing them. for example if i have the string which looks like this: var1;var2;

how can i get var1 to become variable and var2 to become a different one.

Thanks in Advance

like image 747
user2035296 Avatar asked Mar 20 '13 21:03

user2035296


People also ask

How do you split a string variable?

split splits the contents of a string variable, strvar, into one or more parts, using one or more parse strings (by default, blank spaces), so that new string variables are generated. Thus split is useful for separating “words” or other parts of a string variable. strvar itself is not modified.

How do you split a variable in R?

The split() function in R can be used to split data into groups based on factor levels. This function uses the following basic syntax: split(x, f, …)

How do you split a variable into two variables in Python?

To split string variables at each whitespace, we can use the split() function with no arguments. The syntax for split() function is split(separator, maxsplit) where the separator specifies the character at which the string should be split. maxsplit specifies the number of times the string has to be split.


1 Answers

The best way to split a variable into an array (or as close to an array as Windows batch can imitate) is to put the variable's value into a format that can be understood by a for loop. for without any switches will split a line word-by-word, splitting at csv-type delimiters (comma, space, tab or semicolon).

This is more appropriate than for /f, which loops line-by-line rather than word-by-word, and it allows splitting a string of an unknown number of elements.

Here's basically how splitting with a for loop works.

setlocal enabledelayedexpansion
set idx=0
for %%I in ("%var:;=","%") do (
    set "var[!idx!]=%%~I"
    set /a "idx+=1"
)

The important part is the substitution of ; into "," in %var%, and enclosing the whole thing in quotation marks. Indeed, this is the most graceful method of splitting the %PATH% environment variable for example.

Here's a more complete demonstration, calling a subroutine to split a variable.

@echo off
setlocal enabledelayedexpansion

set string=one;two;three;four;five;

:: Uncomment this line to split %PATH%
:: set string=%PATH%

call :split "%string%" ";" array

:: Loop through the resulting array
for /L %%I in (0, 1, %array.ubound%) do (
    echo array[%%I] = !array[%%I]!
)

:: end main script
goto :EOF


:: split subroutine
:split <string_to_split> <split_delimiter> <array_to_populate>
:: populates <array_to_populate>
:: creates arrayname.length (number of elements in array)
:: creates arrayname.ubound (upper index of array)

set "_data=%~1"

:: replace delimiter with " " and enclose in quotes
set _data="!_data:%~2=" "!"

:: remove empty "" (comment this out if you need to keep empty elements)
set "_data=%_data:""=%"

:: initialize array.length=0, array.ubound=-1
set /a "%~3.length=0, %~3.ubound=-1"

for %%I in (%_data%) do (
    set "%~3[!%~3.length!]=%%~I"
    set /a "%~3.length+=1, %~3.ubound+=1"
)
goto :EOF

Here's the output of the above script:

C:\Users\me\Desktop>test.bat
array[0] = one
array[1] = two
array[2] = three
array[3] = four
array[4] = five

Just for fun, try un-commenting the set string=%PATH% line and let the good times roll.

like image 51
rojo Avatar answered Nov 17 '22 13:11

rojo