header
header Register : : Login header
header
divider
menuleft
menuright
submenu
left

[August 25th, 2008] Check the home page regarding PowerShell related news from a brand new sponsor: Idera

-match returning whole array instead of element
Last Post 13 Aug 2008 03:59 PM by halr9000. 7 Replies.
Printer Friendly
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
n1ckml007User is Offline
New Member
New Member
Posts:15

--
12 Aug 2008 03:29 PM  

$drunk = @( 'berry', 'ferry', 'merry')

$drunk |where {$drunk -match 'b'}

returns the whole array:

berry
ferry
merry

 

I thought -match was supposed to just return the matching elements?

EdengundamUser is Offline
New Member
New Member
Posts:3

--
12 Aug 2008 04:02 PM  
Hi,
$_ refers to the pipeline object.
you access $drunk in the scriptblock of where-object and the -match will return 'berry'. 'berry' will be converted to the Bool type and it is $true. So all of elements in the $drunk will be displayed on the console.
n1ckml007User is Offline
New Member
New Member
Posts:15

--
12 Aug 2008 04:15 PM  

Thanks for the concise answer:


$drunk = @( 'berry', 'ferry', 'merry')

$drunk |where {$_ -match 'b'}

returns berry

 

now why doesn't this return anything?

 

$funk = 'b','f'

$drunk = @( 'berry', 'ferry', 'merry')

$drunk | where {$_ -match $funk}

 

glnsizeUser is Online
New Member
New Member
Posts:84

--
13 Aug 2008 02:53 AM  

$drunk|?{$_ -match 'f','b'}

will fail to find a match... that comma is read literaly not as an or as you intended it.

$drunk | ? { $_ -match "b" -or $_ -match "b"}
will return as intended but that’s a lot of typing. Instead, remember -match is a regex as such can process anything you could normally do in an (if|switch) statement.  With that in mind try,

$funk = "^[b,f]"
$drunk|?{$_ -match $funk}

^ dictates first character
[] says match any char contained within []
, dictates or So...

"^[b,f]" really means, where first character is equal to "b" or "f". 

help about_Regular_Expression is a great place to reference when you’re getting erroneous results.

~Glenn
n1ckml007User is Offline
New Member
New Member
Posts:15

--
13 Aug 2008 01:08 PM  
So say in the above example if $funk was an array in memory (of email addresses), I would need to change the array to something that was regex-formatted?

It sounds like "diff -pass" is going to work better for me than -match for selecting objects(elements) in an array based on elements in another array.
EdengundamUser is Offline
New Member
New Member
Posts:3

--
13 Aug 2008 02:02 PM  
Try this:

$func = 'b','f'
$ofs = '|'
$filter = "$func"
$drunk | ?{ $_ -match $filter }
n1ckml007User is Offline
New Member
New Member
Posts:15

--
13 Aug 2008 03:15 PM  

Hey,

 

What does setting $filter equal to a variable do?

 

What does setting the pipe equal to a variable do?

 

Thanks,

 

-N1ck

halr9000User is Offline
Basic Member
Basic Member
Posts:316

--
13 Aug 2008 03:59 PM  
1. Makes things cleaner for the very next line. Good style of scripting to use when you want the next guy to be able to figure out what the heck you wrote.

2. $ofs is a special variable, read this: http://blogs.msdn.com/powershell/archive/2006/07/15/What-is-OFS.aspx
You are not authorized to post a reply.

Active Forums 4.1
right
   
footer Sponsored by Quest Software • SAPIEN Technologies • ShellTools, LLC • Microsoft Windows Server 2008 footer
footer