![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Issue with using invoke-expression on a char Trying to add 2 different numbers together using something like: invoke-expression 3${a}4 What I'd really like to have working though is something like this: invoke-expression 3${a[0]}4 Now, $a.gettype() and $a[0].gettype() come out differently, and I don't know if/how to make $a[0] into a string like $a would be. [C:\psh] 121> $a="+" [C:\psh] 122> $a.gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True String System.Object [C:\psh] 123> $a[0].gettype() IsPublic IsSerial Name BaseType -------- -------- ---- -------- True True Char System.ValueType [C:\psh] 124> invoke-expression 3${a}4 7 [C:\psh] 125> invoke-expression 3${a[0]}4 34 [C:\psh] |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char Is this what you had in mind? $op = "+" invoke-expression "3 $op 3" 6 -- William Stacey [C# MVP] PCR concurrency library: www.codeplex.com/pcr PSH Scripts Project www.codeplex.com/psobject "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message news:%237bG9yTUHHA.3592@TK2MSFTNGP03.phx.gbl... | Trying to add 2 different numbers together using something like: | invoke-expression 3${a}4 | | What I'd really like to have working though is something like this: | invoke-expression 3${a[0]}4 | | Now, $a.gettype() and $a[0].gettype() come out differently, and I don't know | if/how to make $a[0] into a string like $a would be. | | [C:\psh] | 121> $a="+" | [C:\psh] | 122> $a.gettype() | | IsPublic IsSerial Name BaseType | -------- -------- ---- -------- | True True String System.Object | | [C:\psh] | 123> $a[0].gettype() | | IsPublic IsSerial Name BaseType | -------- -------- ---- -------- | True True Char System.ValueType | | [C:\psh] | 124> invoke-expression 3${a}4 | 7 | [C:\psh] | 125> invoke-expression 3${a[0]}4 | 34 | [C:\psh] | | |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char > Trying to add 2 different numbers together using something like: > invoke-expression 3${a}4 > > What I'd really like to have working though is something like this: > invoke-expression 3${a[0]}4 > > Now, $a.gettype() and $a[0].gettype() come out differently, and I don't > know if/how to make $a[0] into a string like $a would be. Fixed... I just added an extra bit of logic to workaround this: $b=[string]$a[0] Marco |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char Is this more along the lines of what you're looking for? PS (1) > $ops = "+","-","*","/" # array of operators PS (2) > 0..3 | foreach { invoke-expression "9 $($ops[$_]) 4" } # evaluate the expression for each operator in the array... 13 5 36 2.25 PS (3) > If you want to expand an expression in a string that is more complex than a simple variable, then you have to put the expression in $( ... ). Of course you could also do it this way :-) PS (4) > foreach ($op in $ops) { invoke-expression "9 $op 4" } 13 5 36 2.25 PS (5) > -bruce -- Bruce Payette [MSFT] Windows PowerShell Technical Lead Microsoft Corporation This posting is provided "AS IS" with no warranties, and confers no rights. Visit the Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell Visit the Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scr.../hubs/msh.mspx My Book: http://manning.com/powershell "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message news:eIuuEiWUHHA.4276@TK2MSFTNGP02.phx.gbl... >> Trying to add 2 different numbers together using something like: >> invoke-expression 3${a}4 >> >> What I'd really like to have working though is something like this: >> invoke-expression 3${a[0]}4 >> >> Now, $a.gettype() and $a[0].gettype() come out differently, and I don't >> know if/how to make $a[0] into a string like $a would be. > > Fixed... > > I just added an extra bit of logic to workaround this: > $b=[string]$a[0] > > Marco > |
My System Specs![]() |
| | #5 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char Speaking of the range operator... It seems to me, I would like to use it like so: $a = 1,2,30..50, 60..100 Likewise: $a[1,3..5,9..100] -- William Stacey [C# MVP] "Bruce Payette [MSFT]" <brucepay@microsoft.com> wrote in message news:OBeegLZUHHA.4796@TK2MSFTNGP05.phx.gbl... | Is this more along the lines of what you're looking for? | | PS (1) > $ops = "+","-","*","/" # array of operators | PS (2) > 0..3 | foreach { invoke-expression "9 $($ops[$_]) 4" } # evaluate | the expression for each operator in the array... | 13 | 5 | 36 | 2.25 | PS (3) > | | If you want to expand an expression in a string that is more complex than a | simple variable, then you have to put the expression in $( ... ). | | Of course you could also do it this way :-) | | PS (4) > foreach ($op in $ops) { invoke-expression "9 $op 4" } | 13 | 5 | 36 | 2.25 | PS (5) > | | -bruce | | -- | Bruce Payette [MSFT] | Windows PowerShell Technical Lead | Microsoft Corporation | This posting is provided "AS IS" with no warranties, and confers no rights. | | Visit the Windows PowerShell Team blog at: | http://blogs.msdn.com/PowerShell | Visit the Windows PowerShell ScriptCenter at: | http://www.microsoft.com/technet/scr.../hubs/msh.mspx | My Book: http://manning.com/powershell | | "Marco Shaw" <marcoDOTshaw_@_gmailDOTcom> wrote in message | news:eIuuEiWUHHA.4276@TK2MSFTNGP02.phx.gbl... | >> Trying to add 2 different numbers together using something like: | >> invoke-expression 3${a}4 | >> | >> What I'd really like to have working though is something like this: | >> invoke-expression 3${a[0]}4 | >> | >> Now, $a.gettype() and $a[0].gettype() come out differently, and I don't | >> know if/how to make $a[0] into a string like $a would be. | > | > Fixed... | > | > I just added an extra bit of logic to workaround this: | > $b=[string]$a[0] | > | > Marco | > | | |
My System Specs![]() |
| | #6 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char "William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message news:eUOldRZUHHA.1208@TK2MSFTNGP03.phx.gbl... > Speaking of the range operator... It seems to me, I would like to use it > like so: > $a = 1,2,30..50, 60..100 > > Likewise: > $a[1,3..5,9..100] > You can. Try this: 1,2+30..50+60..100 -- Keith |
My System Specs![]() |
| | #7 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char I saw that, but that made me feel dirty :-) As an operator, not sure why this could not be made to work as I shown. Good workaround, thanks. -- William Stacey [C# MVP] "Keith Hill" <r_keith_hill@mailhot.nospamIdotcom> wrote in message news:0B7F9BD0-CCA2-4686-9D39-40B8E99E0432@microsoft.com... | "William Stacey [C# MVP]" <william.stacey@gmail.com> wrote in message | news:eUOldRZUHHA.1208@TK2MSFTNGP03.phx.gbl... | > Speaking of the range operator... It seems to me, I would like to use it | > like so: | > $a = 1,2,30..50, 60..100 | > | > Likewise: | > $a[1,3..5,9..100] | > | | You can. Try this: | | 1,2+30..50+60..100 | | -- | Keith | |
My System Specs![]() |
| | #8 (permalink) |
| Guest | Re: Issue with using invoke-expression on a char > If you want to expand an expression in a string that is more complex than > a simple variable, then you have to put the expression in $( ... ). > > Of course you could also do it this way :-) > > PS (4) > foreach ($op in $ops) { invoke-expression "9 $op 4" } Thanks Bruce. That's nice to know. I really have to sit down and read your book! |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Invoke-Expression issue | RG | PowerShell | 4 | 01-14-2008 07:37 PM |
| Possible invoke-expression issue? | Lanwin | PowerShell | 9 | 01-07-2008 04:02 PM |
| CTP: Invoke-Expression bug? | Shay Levi | PowerShell | 2 | 12-06-2007 01:45 AM |
| Invoke-expression vs & | Leo Tohill | PowerShell | 4 | 11-29-2007 04:49 AM |
| Issue: Invoke-Expression with $args in the expression | =?Utf-8?B?Um9tYW4gS3V6bWlu?= | PowerShell | 1 | 09-22-2006 09:17 AM |