0

I have the following code:

get-content C:\file.txt | Foreach{($_ | Select-String "$" -all).Matches | measure | select count} 

I want to find the number of $ in the file on each line, which works successfully, I am given a tabled count of the number of each character per line. However, I want to output each as a value to an array and perform an arithmetic operation on the count. As it stands everything I've tried has given me a multidimensional array in stead of an array of integers. For example I've tried

$counts = @(get-content C:\ampersand.txt | Foreach{($_ | Select-String "&" -all).Matches | measure | select count} )

but that just spits out a multidimensional array which I can't perform an arithmetic operation on

3
  • This thing I see wrong of the top of my head is that your not returning an integer array but an object array with a count parameter. | select -expand count help? Do you have an example of what you are trying to do with the output?
    – Matt
    Commented Jun 24, 2015 at 16:55
  • I want to output and be able to do something like foreach($count in $counts){$count %= 2} Commented Jun 24, 2015 at 17:01
  • 1
    Ok. that probably is what I said then. Try the code in my comment.
    – Matt
    Commented Jun 24, 2015 at 17:21

1 Answer 1

2

I think this is just one of the PowerShell gotcha's. $Counts is not an integer array but an object array with a count property.

get-content C:\file.txt | Foreach{($_ | Select-String "$" -all).Matches | measure | select-object -ExpandProperty count}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.