0

Am calling for loop and trying to append the $i value along with a variable called inside the function. not sure how to do this. Everytime I get a error saying "Unexpected token 'i' in expression or statement." any suggestions/idea please.

Thanks to Chris. His code works perfectly..

code :

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)"
}
5
  • What are you attempting to do with the variables when you use $M$i? Concatenation as a string? If so, you need to make it a string "$M$i". If you are trying to create a variable based on $i you need to use New-Variable "M$i". Commented Aug 18, 2016 at 10:21
  • You might be better served by adding an example of what you're attempting to parse with the recursive function. I've no problem passing $i around to control depth, but your variable assignment is confusing without context. Commented Aug 18, 2016 at 10:31
  • @Chris, am trying to create a variable based in $i, so guess, I need to try out your suggestion ie, New-Variable "M$i"..let me try now Commented Aug 18, 2016 at 11:12
  • I think you need to step back from this method for a minute, perhaps it's the wrong path. Adding a lot of New-Variable / Get-Variable is going to make it even more messy and confusing. Is there a reason you need to assign like that? Output can be returned via the output pipeline immediately without assignment, working-values can be passed into Get-Data to aid recursion. Commented Aug 18, 2016 at 11:34
  • Perfect. Thanks Chris. your code does what I was trying to achieve. Many Thanks.. I have updated my original post so, it may be help for someone.. Commented Aug 18, 2016 at 12:01

1 Answer 1

1

I can't tell you if this will work, but I would not rely on globally assigned variables to pass information out of a function.

I suspect it may need a bit of work around construction of the parameters for 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)"
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.