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 - Array indexing: Want to say "Item #2 through the rest of the array."

Reply
 
Old 12-15-2008   #1 (permalink)
Kevin Buchan


 
 

Array indexing: Want to say "Item #2 through the rest of the array."

Let's say I have a text file called 'GetContentTest.txt' with the
number 1 through 10 in it with each number on its own line.

I'd like to skip over line 1 and just read lines 2 through 10. I know
that Get-Content does not have a parameter to start at line 2, but for
my purposes, I don't care 'cause this file will always be reasonably
small.

I *thought* I could do something like this:
PS > (gc getcontenttest.txt)[1..-1]

I know that the '1' indicates the seond row in the file and I know
that '-1' indicates the last row, but when I try to write a sequence
like this, I get:
2
1
10

That's clearly, the line at index 1, then the line at index 0, then
the line at index -1, so it appears to be going from 1 to -1
backwards. Is there a syntax that I can use to accomplish this?

This worked, but I was hoping for something more "Powershellish".
$a = gc GetContentTest.txt; $a[1..$($a.length-1)]

Thanks!

--
Kevin Buchan
kevin.buchan@xxxxxx[nospam]sanders.com

My System SpecsSystem Spec
Old 12-15-2008   #2 (permalink)
Josh Einstein


 
 

Re: Array indexing: Want to say "Item #2 through the rest of the array."

In PowerShell 2.0, the Select-Object cmdlet has a -Skip parameter.

In the meantime, you can do this:

filter Skip-Object([int]$Count) {
$ObjectCount += 1
if ( $ObjectCount -gt $Count ) { $_ }
}

1..10 | Skip-Object 2
3
4
5
6
7
8
9
10

Josh Einstein

"Kevin Buchan" <kevin.buchan@xxxxxx[PlsDon'tSpam]sanders.com> wrote in
message news:atqdk4l5ottva8e67smjhnf3ileek4grj3@xxxxxx
Quote:

> Let's say I have a text file called 'GetContentTest.txt' with the
> number 1 through 10 in it with each number on its own line.
>
> I'd like to skip over line 1 and just read lines 2 through 10. I know
> that Get-Content does not have a parameter to start at line 2, but for
> my purposes, I don't care 'cause this file will always be reasonably
> small.
>
> I *thought* I could do something like this:
> PS > (gc getcontenttest.txt)[1..-1]
>
> I know that the '1' indicates the seond row in the file and I know
> that '-1' indicates the last row, but when I try to write a sequence
> like this, I get:
> 2
> 1
> 10
>
> That's clearly, the line at index 1, then the line at index 0, then
> the line at index -1, so it appears to be going from 1 to -1
> backwards. Is there a syntax that I can use to accomplish this?
>
> This worked, but I was hoping for something more "Powershellish".
> $a = gc GetContentTest.txt; $a[1..$($a.length-1)]
>
> Thanks!
>
> --
> Kevin Buchan
> kevin.buchan@xxxxxx[nospam]sanders.com
My System SpecsSystem Spec
Old 12-19-2008   #3 (permalink)
Kevin Buchan


 
 

Re: Array indexing: Want to say "Item #2 through the rest of the array."

Thanks, I like that solution!

For loops feel so "Iterative" that I tend not to remember they exist
in PoSh. :-)

I find that I try to force everything into a pipeline solution with
PowerShell.

--
Kevin Buchan
kevin.buchan@xxxxxx[nospam]sanders.com


On Tue, 16 Dec 2008 16:31:07 -0400, "Marco Shaw [MVP]"
<marco.shaw@_NO_SPAM_gmail.com> wrote:
Quote:

>
Quote:

>> This worked, but I was hoping for something more "Powershellish".
>> $a = gc GetContentTest.txt; $a[1..$($a.length-1)]
>
>Is this better?
>for($i=1;$i -lt $a.count;$i++){$a[$i]}
>
>Your solution looks PowerShellish enough to me... ;-)
>
>Marco
My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
problem exporting OU users with filter = array("user") command VB Script
Big array - remove item - too slow PowerShell
RAID array degraded - "missing hard drive" Vista hardware & devices
When I start my computer I get an "array index error" 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