Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Can someone explain what this piece of code is doing?

This is code I found that splits an array into odd or even results.

$f=0; $array | % { if($f = !$f) { $_ } }

The part I don’t understand is the if statement. Specifically $f=!$f.

The way I’m reading it is if(0 = not 0). The result should always be true since it’s using = and not -eq. I’m sure it’s something simple I’m missing, but I can’t find any info on it.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If anyone has any good resources to read that goes further into this type of logic I’d also love to read more on it.

>Solution :

It’s showing every odd array element. $f goes back and forth from $true to $false. The value of the assignment is what’s on the left side. I would start with $f = $false, to make it a little more clear.

!0
True

!$true
False

$array = 1..10
$f=0; $array | % { if($f = !$f) { $_ } }
1
3
5
7
9
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading