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

Array Slicing - Powershell compared to Python

Closed Thread
 
Thread Tools Display Modes
Old 05-04-2007   #1 (permalink)
Staffan Gustafsson
Guest


 

Array Slicing - Powershell compared to Python

Hi,

I guess this is a question for the Powershell team.

When comparing array slicing with for example Python, I find that Powershell
does not support doing what i most often use slicing for, ie "Get all but
the first/last n items".

In python you can write
somelist[:-3]
to get all but the last three (and the empty list if there are less than
three elements in the list)

In powershell you have to write:

if ($somelist.Length -gt 3){
@()
}
else {
$somelist[0..($somelist.Length -4)]
}

I would be quite hard pressed to call the Powershell way more elegant. Are
there any plans for vNext to improve this area?

Regards,
/Staffan



Old 05-05-2007   #2 (permalink)
Jeffrey Snover [MSFT]
Guest


 

Re: Array Slicing - Powershell compared to Python

Howdy Staffan!
I have 2 thoughts on this:
1) We have a really big list of amazing features we'd like to get
implemented in Vnext and are in the process of deciding which ones we won't
do (to ship is to choose). This doesn't "pop" as a compelling feature to
me.

2) We are very customer driven. The fact that you've asked about it means a
lot. Please submit this as a request via Connect and then other people can
vote on it. We don't have the bandwidth to implement all feature requests
but we do have bandwidth to implement some of the ones that the community
asks for the most.

Thanks for asking!
--
Jeffrey Snover [MSFT]
Windows Management Partner Architect
Microsoft Corporation
This posting is provided "AS IS" with no warranties, no confers 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


Old 05-07-2007   #3 (permalink)
aaronlerch@gmail.com
Guest


 

Re: Array Slicing - Powershell compared to Python

You could do this with type extensions (I posted about this example on
my blog here: http://www.aaronlerch.com/blog/2007/...xtensions.html)

In this case, something like this should work:

<Types>
<Type>
<Name>System.Array</Name>
<Members>
<ScriptMethod>
<Name>Slice</Name>
<Script>
if ($args[0] -eq $null) { $start = 0 }
elseif ($args[0] -lt 0) { $start = $this.Length + [int]$args[0] }
elseif ($args[0] -ge 0) { $start = [int]$args[0] }

if ($args[1] -eq $null) { $end = $this.Length - 1 }
elseif ($args[1] -lt 0) { $end = $this.Length + [int]$args[1] -
1 }
elseif ($args[1] -ge 0) { $end = [int]$args[1] - 1 }

if ($start -lt 0) { $start = 0 }
elseif ($start -gt $this.Length) { return @() }

if ($end -gt $this.Length) { $end = $this.Length }
elseif ($end -lt 0) { return @() }

if ($start -ge $end) { return @() }
else { return $this[$start..$end] }
</Script>
</ScriptMethod>
</Members>
</Type>
</Types>

On May 5, 12:11 pm, "Jeffrey Snover [MSFT]" <jsno...@microsoft.com>
wrote:
> Howdy Staffan!
> I have 2 thoughts on this:
> 1) We have a really big list of amazing features we'd like to get
> implemented in Vnext and are in the process of deciding which ones we won't
> do (to ship is to choose). This doesn't "pop" as a compelling feature to
> me.
>
> 2) We are very customer driven. The fact that you've asked about it means a
> lot. Please submit this as a request via Connect and then other people can
> vote on it. We don't have the bandwidth to implement all feature requests
> but we do have bandwidth to implement some of the ones that the community
> asks for the most.
>
> Thanks for asking!
> --
> Jeffrey Snover [MSFT]
> Windows Management Partner Architect
> Microsoft Corporation
> This posting is provided "AS IS" with no warranties, no confers 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



Closed Thread

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
PowerShell param array Daniel Jameson PowerShell 2 1 Week Ago 11:47 PM
Re: Running python scripts under PowerShell Marco Shaw PowerShell 0 09-24-2007 11:11 AM
Re: Running python scripts under PowerShell Brandon Shell PowerShell 0 09-24-2007 10:48 AM
how to assign values to array and how to create array via variable Frank PowerShell 1 03-13-2007 05:18 PM
Using a WMI string array in Powershell GordT. PowerShell 1 01-11-2007 12:33 PM








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

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 47 48 49 50