2016-08-18 3 views
0

루프를 호출하고 함수 내부에서 호출 된 변수와 함께 $ i 값을 추가하려고합니다. 이 작업을 수행하는 방법을 모릅니다. 매번 "예기치 않은 토큰 'i'이 표현식이나 문장에 오류가 발생합니다." 어떤 제안/아이디어 바랍니다.append forloop iterator 변수 값

Chris에게 감사드립니다. 그의 코드는 .. 완벽하게 작동

코드 :이 작동하는지

function Get-Data { 
    param(
     # Consider giving this a more meaningful name 
     [Int]$i 
    ) 

    # Assigns the value in the first index from -split to $null 
    # and the value in the second index to $msgs. 
    $null, $msgs = (b2b.exe -readparams "msgs${i}data" | Select-Object -Skip 1 -First 1) -split '=' 
    $null, $bytes = (b2b.exe -readparams "bytes${i}data" | Select-Object -Skip 1 -First 1) -split '=' 

    [PSCustomObject]@{ 
     MData = $msgs.Trim() 
     BData = $bytes.Trim() 
    } 
} 

for ($i=0; $i-le 3; $i++) { 
    $data = Get-Data $i 
    write-host "for MData$i $($data.MData)" 
    write-host "for BData$i $($data.BData)" 
} 
+0

'$ M $ i'을 (를) 사용할 때 무엇을하려고합니까? 문자열로 연결? 그렇다면 문자열을 "$ M $ i"'로 만들어야합니다. $ i에 기반한 변수를 만들려고한다면'New-Variable "M $ i"'를 사용할 필요가 있습니다. –

+0

재귀 함수로 구문 분석하려는 예제를 추가하면 더 효과적 일 수 있습니다. 나는 $ i를 깊이 제어하는 ​​데 아무런 문제가 없지만, 변수 할당은 문맥없이 혼란 스럽다. –

+0

@Chris, $ i에 기반한 변수를 만들려고합니다. 그래서 새 변수 "M $ i"를 제안해야합니다. – HULK

답변

1

내가 당신에게 말할 수 없다,하지만 난 함수에서 정보를 전달하는 전 세계적으로 할당 된 변수에 의존하지 않을 것입니다.

b2b.exe에 대한 매개 변수 생성과 관련하여 약간의 문제가 발생할 것으로 생각됩니다.

function Get-Data { 
    param(
     # Consider giving this a more meaningful name 
     [Int]$i 
    ) 

    # Assigns the value in the first index from -split to $null 
    # and the value in the second index to $msgs. 
    $null, $msgs = (b2b.exe -readparams "msgs${i}data" | Select-Object -Skip 1 -First 1) -split '=' 
    $null, $bytes = (b2b.exe -readparams "bytes${i}data" | Select-Object -Skip 1 -First 1) -split '=' 

    [PSCustomObject]@{ 
     MData = $msgs.Trim() 
     BData = $bytes.Trim() 
    } 
} 

for ($i=0; $i-le 3; $i++) { 
    $data = Get-Data $i 
    write-host "for MData$i $($data.MData)" 
    write-host "for BData$i $($data.BData)" 
}