Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Dynamic statements?

Reply
 
Old 06-16-2009   #1 (permalink)
gurbao


 
 

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 SpecsSystem Spec
Old 06-16-2009   #2 (permalink)
Rob Campbell


 
 

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 SpecsSystem Spec
Old 06-16-2009   #3 (permalink)
Kryten


 
 

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 SpecsSystem Spec
Reply

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


Vista Forums is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media Ltd

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46