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 - trouble with count / length of array filled from filtered ls

Reply
 
Old 08-31-2009   #1 (permalink)
Tim


 
 

trouble with count / length of array filled from filtered ls

hi,

i have been scripting PS off and on over the past year or so. the
other day i ran across a bug in a script of mine that I'm having
trouble understanding. hoping someone can help me get a handle on it.

if I do the following interactively in a powershell window:
$alist = ls . | where {$_.name -match ".sql$"}
$alist.count
it works just fine and returns 2 in this case.

trying to do the same thing in a script (the main difference ist that
I've passed the pattern as a parameter
param(
[string] $match = ""
)
) i end up with an array that appears to contain an item when I echo
it, but shows a count of 0.

any idea what could be going wrong here? i borrowed the technique from
a different script of mine which has been working fine for months.

cheers,

Tim Hanson

My System SpecsSystem Spec
Old 08-31-2009   #2 (permalink)
Shay Levy [MVP]


 
 

Re: trouble with count / length of array filled from filtered ls

Hi Tim,

the diollar sign is a regex metacharacter (denotes end of line). You need
to escape it for -match to succssed:


ls . | where {$_.name -match ".sql\$"}


You can also get the files without piping to where-object or without using
regex at all:

ls *.sql



---
Shay Levy
Windows PowerShell MVP
http://blogs.microsoft.co.il/blogs/ScriptFanatic
PowerShell Toolbar: http://tinyurl.com/PSToolbar



T> hi,
T>
T> i have been scripting PS off and on over the past year or so. the
T> other day i ran across a bug in a script of mine that I'm having
T> trouble understanding. hoping someone can help me get a handle on it.
T>
T> if I do the following interactively in a powershell window:
T> $alist = ls . | where {$_.name -match ".sql$"}
T> $alist.count
T> it works just fine and returns 2 in this case.
T> trying to do the same thing in a script (the main difference ist that
T> I've passed the pattern as a parameter
T> param(
T> [string] $match = ""
T> )
T> ) i end up with an array that appears to contain an item when I echo
T> it, but shows a count of 0.
T>
T> any idea what could be going wrong here? i borrowed the technique
T> from a different script of mine which has been working fine for
T> months.
T>
T> cheers,
T>
T> Tim Hanson
T>


My System SpecsSystem Spec
Reply

Thread Tools


Similar Threads
Thread Forum
Trying to figure out how to get object count of unique filtered pr PowerShell
Trouble with Array VB Script
Newsgroups 'filtered' Live Mail
Bug in array.count? PowerShell
Junk email does not get filtered. Vista mail


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