Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Issue with using invoke-expression on a char

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 02-15-2007   #1 (permalink)
Marco Shaw
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 SpecsSystem Spec
Old 02-15-2007   #2 (permalink)
William Stacey [C# MVP]
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 SpecsSystem Spec
Old 02-15-2007   #3 (permalink)
Marco Shaw
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 SpecsSystem Spec
Old 02-16-2007   #4 (permalink)
Bruce Payette [MSFT]
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 SpecsSystem Spec
Old 02-16-2007   #5 (permalink)
William Stacey [C# MVP]
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 SpecsSystem Spec
Old 02-16-2007   #6 (permalink)
Keith Hill
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 SpecsSystem Spec
Old 02-16-2007   #7 (permalink)
William Stacey [C# MVP]
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 SpecsSystem Spec
Old 02-16-2007   #8 (permalink)
Marco Shaw
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 SpecsSystem Spec
Closed Thread

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


Update your Vista Drivers Update Your Drivers Now!!

Vistax64.com 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 2005-2008