![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | Dynamic statements? Hi, I am just curious if this can be done. Depending on the value of a variable, can I use different comparison operators? e.g. if ($c -eq 1){$operator = "-match"}else{$operator = "-notmatch"} foreach ($a in $coll){ if ($a $operator $b) { # some common code here. } } I hope you understand what I am asking by the above example... Regards, |
My System Specs![]() |
| | #2 (permalink) |
| | RE: Dynamic statements? This seems to work: if ($c -eq 1){$operator = "-match"}else{$operator = "-notmatch"} foreach ($a in $coll){ if (iex $('"$a"' + $operator + '"$b"') { # some common code here. "gurbao" wrote: Quote: > Hi, > > I am just curious if this can be done. > > Depending on the value of a variable, can I use different comparison > operators? > > e.g. > > if ($c -eq 1){$operator = "-match"}else{$operator = "-notmatch"} > > foreach ($a in $coll){ > if ($a $operator $b) { > # some common code here. > } > } > > > I hope you understand what I am asking by the above example... > > Regards, > |
My System Specs![]() |
| | #3 (permalink) |
| | Re: Dynamic statements? Hi, Just one way - I'm sure there are faster and better but maybe it's close to what you need. function test-expressions ($integer) { if ( $integer -le 599 ) { $op = "-match" } elseif ( $integer -gt 599 ) { $op = "-notmatch" } $statement = "$integer $op 599" Invoke-Expression $statement } test-expressions 599 test-expressions 600 test-expressions 4 Cheers, Stuart On 16 June, 11:52, gurbao <aud...@xxxxxx> wrote: Quote: > Hi, > > I am just curious if this can be done. > > Depending on the value of *a variable, can I use different comparison > operators? > > e.g. > > if ($c -eq 1){$operator = "-match"}else{$operator = "-notmatch"} > > foreach ($a in $coll){ > * * if ($a $operator $b) { > * * * * # some common code here. > * * } > > } > > I hope you understand what I am asking by the above example... > > Regards, |
My System Specs![]() |
![]() |
| Thread Tools | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Using select statements | VB Script | |||
| test-path and if statements within my foreach loop | PowerShell | |||
| Breaking change in CTP evaluation of statements? | PowerShell | |||
| ADO.Net / Multiple SQL Statements in One Command | .NET General | |||
| Are These Statements True? | Vista installation & setup | |||