Change values of an array of pscustomobject's with a for-loop - powershell

I have tried to create a array of custom objects (pscustomobject) and now i tried to change some values of the different custom objects with an for-loop. But it doesn't seem to work. Here is what i tried:
$obj = #([pscustomobject]#{value=0;type="D";used=$false})
$arr1 = #($obj) * 10
for($v = 0; $v -lt 4; $v++){
$arr1[$v].value = ($v+1)
$arr1[$v].type ="bubble"
}
The result is:
value type used
----- ---- ----
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
4 bubble False
But i expected that the result will be:
value type used
----- ---- ----
1 bubble False
2 bubble False
3 bubble False
4 bubble False
4 D False
4 D False
4 D False
4 D False
4 D False
4 D False
This is only a snipped, and i was only trying something out. But i am a bit annoyed that i don't get it... Sorry i think it's a easy think but don't see whats worong.... i am still a PS noob... :-/
EDIT: PSv3 is used, but i think it doesn't matter...

This:
$arr1 = #($obj) * 10
is not creating 10 new objects. It created 10 references to the same object.
Note the difference if you do this:
$arr1 = 1..10 |% {[pscustomobject]#{value=0;type="D";used=$false}}
for($v = 0; $v -lt 4; $v++){
$arr1[$v].value = ($v+1)
$arr1[$v].type ="bubble"
}
$arr1
value type used
----- ---- ----
1 bubble False
2 bubble False
3 bubble False
4 bubble False
0 D False
0 D False
0 D False
0 D False
0 D False
0 D False

Related

Trading View plotting false statements?

I'm having an issue with pinescript plotting false statements as part of the plot function. For example, I can type a basic true false formula such as (close < 30) and set my plot to put a lime background against all candles in which the statement is true. But trading view has been acting up and not distinguishing true and false in some situations. Why is this happening? Can anyone help?
if close < 30
longstay := false
shortstay := true
debugger = shortstay ? color.new(color.lime, 30) : na
bgcolor(debugger)
//#version=5
indicator("test",overlay=true)
var longstay=true
var shortstay=false
if close < 30
longstay := false
shortstay := true
else
longstay := true
shortstay := false
debugger = shortstay ? color.new(color.lime, 30) : na
bgcolor(debugger)
You have been setting the value of variable shortstay as true when close was below 30
But it remained true for all the further candles
So I have changed its value to false when the close is above 30 and now it will only show colored bars when the condition is true

de'morgans law boolean translation discrepency. what am i downg wrong?

demorgans law states in this article
https://ryanstutorials.net/boolean-algebra-tutorial/boolean-algebra-laws.php#introduction
that NOT(P & K) = NOT(P) OR NOT(K)
so i did a little testing with 1's and zeros
NOT(1 AND 1) = false
NOT(1 AND 0) = false
NOT(0 AND 1) = false
NOT(O AND 0) = true
so then i did the second expression
NOT(1) OR NOT(O) = TRUE
NOT(0) OR NOT(1) = TRUE
NOT(0) OR NOT(0) = TRUE
NOT(1) OR NOT(1) = FALSE
what am i doing wrong here?
You have to do this table :
P K | Q Q'
0 0 | 0 1
0 1 | 0 1
1 0 | 0 1
1 1 | 1 0
The Q is the Output and the Q' is the Output', 0=false and 1=true;
So, the first column is obtained doing P*K and the second denying Qso P'+K'.

How can I reliably set the backroundimage of a picturebox to be a random color?

I have a fairly simple color randomization snippet that I am using so I can accurately see the bounds of an image matrix I am making on screen. Problem is that I can not get it to work 100% of the time.
$listingImage = [System.Windows.Forms.PictureBox]::new()
...
# Temp color code to help with visual
$listingImage.BackColor = ([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))
The rest of the code follows in the question but that is the heart of what I am trying to do. I have seen it working several times but repeated executions show what seem like, empty, blank or otherwise invisible picture boxes. If I hard code a colour like $listingImage.BackColor = [System.Drawing.Color]::DarkCyan I can see that working fine every time. I want different colours though so I can see how to boxes are lining up with each other.
The code you can use for testing is as follows:
Add-Type -AssemblyName System.Windows.Forms
$imageContainerSize = [Drawing.Size]::new(100,100) # Width, Height
$numberOfImages = [pscustomobject]#{
Horizontal = 5
Vertical = 4
}
$formOverallSize = [Drawing.Size]::new(
$imageContainerSize.Width * $numberOfImages.Horizontal,
$imageContainerSize.Height * $numberOfImages.Vertical
)
$listingImageForm = New-Object System.Windows.Forms.Form
$listingImageForm.Text = $listing.URL
$listingImageForm.Size = $formOverallSize
$listingImageForm.FormBorderStyle = [System.Windows.Forms.FormBorderStyle]::FixedSingle
$listingImageForm.StartPosition = [System.Windows.Forms.FormStartPosition]::CenterScreen
$imageMatrixXOffset = 0
$imageMatrixYOffset = 0
# Load the image place holder image.
$placeholderImagePath = "m:\scripts\test.png"
# $placeholderImage = [system.drawing.image]::FromStream([IO.MemoryStream]::new([System.IO.File]::ReadAllBytes($placeholderImagePath)))
# Create an image matrix from the images provided in a listing group
for ($verticalImageIndex = 0; $verticalImageIndex -lt $numberOfImages.Vertical; $verticalImageIndex++){
for ($horizonalImageIndex = 0; $horizonalImageIndex -lt $numberOfImages.Horizontal; $horizonalImageIndex++){
$listingImage = [System.Windows.Forms.PictureBox]::new()
$listingImage.Size = $imageContainerSize
$listingImage.BorderStyle = [System.Windows.Forms.BorderStyle]::None
$listingImage.SizeMode = [System.Windows.Forms.PictureBoxSizeMode]::CenterImage
$listingImage.Location = [System.Drawing.Point]::new($horizonalImageIndex * $listingImage.Size.Width + $imageMatrixXOffset,
$verticalImageIndex * $listingImage.Size.Height + $imageMatrixYOffset )
# Temp color code to help with visual
$listingImage.BackColor = ([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))
# Place the image based
# $listingImage.Image = $placeholderImage
$listingImage.Tag = "h:$horizonalImageIndex v:$verticalImageIndex"
# Download the image as a memory stream to bypass saving the file
$listingImageForm.Controls.Add($listingImage)
}
}
# Adjust the size of the form to account for the title bar and the width of the form.
$formBorderWidth = ($listingImageForm.Width - $listingImageForm.ClientSize.Width) / 2
$formTitleBarHeight = $listingImageForm.Height – $listingImageForm.ClientSize.Height – 2 * $formBorderWidth
# Adjust for based on previosly calculated values
$listingImageForm.Size.Height = $listingImageForm.Size.Height + $formTitleBarHeight + ($formBorderWidth * 2)
$listingImageForm.Size.Width = $listingImageForm.Size.Width + ($formBorderWidth * 2)
$listingImageForm.Add_Shown({$listingImageForm.Activate()})
[void]$listingImageForm.ShowDialog()
"Form Height : $($listingImageForm.Size.Height)"
"Form Width : $($listingImageForm.Size.Width)"
"Image Height: $($imageContainerSize.Height)"
"Image Width : $($imageContainerSize.Width)"
$listingImageForm.Dispose()
Why is my randomization not working correctly? I really don't think it is the randomization itself since I can run
The problem is with the randomization upperbound.
[System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999)
So this will only generate values of a maximum 999999. The number will never get high enough to change the alpha channel / opacity value of the 'random' colour. If you kept trying that code in the command line you will see the A value is always zero.
([System.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))
R : 10
G : 177
B : 51
A : 0
IsKnownColor : False
IsEmpty : False
IsNamedColor : False
IsSystemColor : False
Name : ab133
A larger testing set to prove the point.
1..1000 | ForEach-Object {
([system.Drawing.Color](Get-Random -Minimum 1 -Maximum 999999))} |
Group-Object -Property A -NoElement
Count Name
----- ----
1000 0
This is why it did not appear to work as the opacity was always zero. I suspect it was periodically working as you might have been changing the randomization code between tests and not correlating the changes.
RGBA Values can be represented as int32 integers values. Setting your randomization to use that as your upper bound will prove more fruitful.
[System.Drawing.Color](Get-Random ([int32]::MaxValue))
Yes, it is possible you could randomize all 0 alpha again but for simple testing that should work just fine.
Click image to embiggen
Or, as Ansgar Wiechers mentions in comments, this might be a friendlier solution to the issue
[Drawing.Color]::FromArgb((Random 256),(Random 256),(Random 256),(Random 256))

Conditional "Or" Statements in MATLAB

I am under the impression that || is the "or" statement in MATLAB. Perhaps someone can explain the confusing behaviour I am seeing:
a = 2;
a == 2 %returns ans = 1 (true)
a == 2 || 3 %returns ans = 1 (true)
a == 3 || 4 %returns ans = 1 (true)??!!
What am I missing here? 'a' is neither 3 or 4, so shouldn't
a == 3 || 4
return ans = 0 (false)?
The expression
a == 3 || 4
is evaluated that way :
a == 3 => false
then
false || 4 => true
if you want to check whether a is equal to 3 or 4 you should write
(a == 3) || (a == 4)
which is evaluated that way
a == 3 => false
then
a == 4 => false
then
false || false => false
Thomas's answer is an excellent explanation of what's going on here; another way that you can compare a variable to multiple answers is using the any() function.
solutions = [3 4];
any(a==solutions);
The a==solutions line creates a matrix the same size as solutions, which contains 1's in indecies which where the conditional is true, and 0's where it is false.
A few more examples:
any(isprime([17:24])); %returns true; 17, 19 and 23 are prime
any(isinteger(sqrt([17:24]))); %(test for square number) returns false; no square numbers in this range
any(mod(magic(3)(:),6)==3); %returns true; 9 mod 6 == 3. Note (:) inserted so that any is evaluated against all entries of the square matrix created by magic
a == 3 || 4 %returns ans = 1 (true)??!!
The reason for the above behaviour is due to the fact that any real number other than '0' in MATLAB is always evaluated to true.
So what is happening here is
The expression a == 3 is evaluated first and found to be false.
Next, expression false || 4 is evaluated.
Since '4' is a real number other than zero, the resulting expression is false || true which is evaluated to true.
To get a desire result use (a == 3) || (a == 4) which is evaluated as false || false which returns false.

Difference between or and xor

My question is operator related. Today I studied about the operators. Having a confusion. In PHP what is the difference between "or" and "xor". I know both of them are related to Boolean expression. But can't find the original difference.
Anyone please help to understand it more clearly.
It has to do with mutual exclusion. xor is exclusive. or is inclusive.
Truth Table Comparison
$x $y ($x or $y) ($x xor $y)
0 0 0 0
1 0 1 1
0 1 1 1
1 1 1 0
Note: the difference in the last case. xor is only true when either $x or $y is true, but not both (as the case for or).
xor means "exclusive or". That is to say, it's or, but with the single change that if both parameters to the operation are true, the answer is false.
A xor B == (A or B) && !(A and B)
The difference is with an input of an even amount of 1's (or true's):
true or true = true
true xor true = false
true or true or true = true
true xor true xor true = true
true or false or true = true
true xor false xor true = false
Truth table of OR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 1
Truth table of XOR:
in1 | in2 | out
-------|-------|-----
0 | 0 | 0
0 | 1 | 1
1 | 0 | 1
1 | 1 | 0
Another way of putting it: OR can be seen as an AND/OR connector. One or both choices can be true or chosen. XOR, on the other side, could be described as a "real" OR. Only one of both choices can be true (or chosen).
one is exclusive or -> xor, and the other is or which means at least one of the conditions are met in a truth table. Or is not mutually exclusive, xor is.
enter image description here
xor is exclusive. or is inclusive