Multiple objects inside variables, output them one by one with powershell - powershell

I got a question, I've been searching high and low for this but cant find an answer (also my coding skills are zero to none)..
In powershell I have 2 variables, for example say $apples and $oranges.
They both contain a list of values that are not yet split and $oranges sometimes contains an empty value, like
$apples = red green blue yellow purple
$oranges = car bike chair shirt
They are in the correct order
Now what I'm trying to achieve is split the values inside the variables, but only print those values inside $apples that have a value greater than 2 characters in $oranges
So that it skips "blue" because in $oranges that spot is empty and the output in the text file would become something like this:
Apples red
Oranges car
Apples green
Oranges bike
Apples yellow
Oranges chair
Apples purple
Oranges shirt
Is there any mastermind out there that could help me?

I edited your question to make any sense, you can't set a string containing spaces to a var without quoting. I still don't know what the meaning of that sentence is.
This script:
$apples = ("red green blue yellow purple").split(' ')
$oranges = ("car bike chair shirt").split(' ')
for ($i=0;$i -le $apples.Length; $i++){
if ($oranges[$i]){
"Apples {0}" -f $apples[$i]
"Oranges {0}" -f $oranges[$i]
""
}
}
Returns this output:
> .\SO_42469662.ps1
Apples red
Oranges car
Apples green
Oranges bike
Apples yellow
Oranges chair
Apples purple
Oranges shirt

Related

Get similar objects in Drools

Assuming I have a list of objects from the type shirt, and each shirt has a color property.
Is there a way to create a rule which will get only shirts of the same color (no matter what the color is)?
You're looking for the collect function. (Link is to the Drools documentation, scroll down a bit to find "collect".) Like its name indicates, it collects things that match a condition.
Let's assume a simple class Shirt with a String color variable. Let's also assume that there are a variety of shirt instances in working memory.
rule "Collect shirts by color"
when
Shirt( $color: color )
$shirts: List() from collect( Shirt( color === $color ) )
then
// $shirts will now contain all shirts of $color
end
This rule will individually consider each shirt, and then collect all of the other shirts which have the same color. So if you have Red, Blue, and Green shirts, you'll enter the right hand side at least once with $shirts of that single color.
Of course the problem here is that you'll trigger the rule on a per-shirt basis instead of on a per-color basis. So if you have two Red shirts, you'll trigger the 'then' clause with all red shirt twice (once for each red shirt, since each red shirt will independently meet the first criteria.)
If you don't mind this, then you can use this as-is. But if you just want your consequences to fire once per shirt color, we have to be a bit more tricksy.
In order for colors to be the first class citizen, we'll need to extract the distinct set (not list!) of shirt colors, and then use those to collect our lists as needed. Here I use the accumulate function; you can read more about that at the same link I shared previously to the Drools documentation (accumulate is directly after collect.)
rule "Get shirt colors"
when
// Get all unique shirt colors
$colors: Set() from accumulate( Shirt( $color: color), collectSet( $color ))
// Get one of those colors
$color: String() from $colors
// Find all shirts of that color
$shirts: List() from collect( Shirt( color === $color ) )
then
// $shirts is all shirts of $color
end
In this second rule, you will only trigger the right hand side once per color because we started by distilling all possible colors into a distinct set of unique ones.
Doing the opposite is even simpler. If all you need to do is confirm there is at least one shirt that is not the same color, we just need to get all of the colors and verify that there's at least 2 different colors present.
rule "At least one shirt of a different color"
when
$colors: Set( size > 1 ) from accumulate(
Shirt( $color: color),
collectSet( $color )
)
then
// $colors contains more than 1 color, so there's at
// least 1 shirt present that is not the same color
// as the rest
end

using histogram to determine colored object presence?

I'm trying to determine if portion of the picture contains red-white striped object (liftramp). If it is present, it looks like this: , and when not like this:
The naive approach was to extract histogram, and count if there is more red pixels than blue/green ones:
use Image::Magick;
my $image = Image::Magick->new;
my $rv = $image->Read($picture);
#$rv = $image->Crop(geometry=>'26x100+484+40');
my #hist_data = $image->Histogram;
my #hist_entries;
# Histogram returns data as a single list, but the list is actually groups of 5 elements. Turn it into a list of useful hashes.
while (#hist_data) {
my ($r, $g, $b, $a, $count) = splice #hist_data, 0, 5;
push #hist_entries, { r => $r, g => $g, b => $b, alpha => $a, count => $count };
}
my $total=0;
foreach my $v (#hist_entries) {
if ($$v{r}>($$v{g}+$$v{b})) { $total +=$$v{count}; }
}
and then comparing if $total > 10 (arbitrary threshold). While that seems to work nice for relatively sunny day (giving 50-180 for presence vs 0-2 for not present), heavy clouds and dusk make the detection always say the liftramp is not present.
I guess there must be smarter way to detect if red-white object is present. So the question is how to do that detection more reliably?
Note that grayish/green background might change with seasons to more of gray-brown or something. I also cannot count on pixel precision as it might move a little (or I'd just crop a 3-4 pixels and look if they are red) - but it should mostly fit it he cropped box.
Another way to do it that would be more insensitive to lighting would be to look for red hues after converting to HSV colorspace. But since red has the same 0 hue as black/gray/white, I would invert the image so that red becomes cyan. So histogram the hue channel after inverting and converting to HSV and look for values at cyan hue near 180 degrees or its equivalent of 50% gray or 128 in the range of 0 to 255. In imagemagick, you would do
convert XqG0F.png -negate -colorspace HSV -channel red -separate +channel -define histogram:unique-colors=false histogram:without_hist.png
convert x5hWF.png -negate -colorspace HSV -channel red -separate +channel -define histogram:unique-colors=false histogram:with_hist.png
So you can see in the second image (for the red bar), there is a substantial broad peak near mid-way i.e., 50% (horizontally), but none in the first image in that region.
You could do an FFT to get the spectrum of each image. The image with the striped bar has a repetitive pattern that should show up in the spectrum. Using ImageMagick:
Without the bar:
convert XqG0F.png -fft +delete -evaluate log 100000 without.png
With bar:
convert x5hWF.png -fft +delete -evaluate log 100000 with.png

How to combine columns of unequal length and make them equal in postgreSQL

If I have two separate tables (names and colors) and have the following columns:
name
John
Amy
Jack
Ray
Jane
Flo
colors
red
green
How do I make it so I get
name color
John red
Amy green
Jack red
Ray green
Jane red
Flo green
I am using postgreSQL-9.2.
Very weird use case, but this should do the trick:
SELECT name || ' ' || color
FROM names, colors
WHERE length(name || ' ' || color) = 8

How to properly count elements in my object collection

I have the following collection;
Collection Property1 Property2
---------------------------------
Coll1 blue hot
Coll1 red cold
Coll1 yellow orange
Coll1 false false
Coll2 dog cat
Coll2 out in
Coll2 house car
Coll3 yellow red
Coll3 brick mortar
Coll3 winter winter
Coll3 summer summer
Coll3 ski snow
Coll3 cyan brown
How do I count how many elements that are in Coll1 (the number of collections will be dynamic so the filter also has to be)?
I guess I am looking for something like how many elements has a unique collection ID
I haven't seen a hashtable in PowerShell that would output as what you have shown in your question, but usually the way is like the following:
Get-Foo | Group-Object Collection -NoElement

NetLogo - How to set turtle color from array

How can I set a turtle's color from an array?
Here's my code but it doesn't work:
let colors array:from-list ["red" "yellow" "blue" "pink"]
set index random 3
let c array:item colors index
set color array:item colors index
Which leads to this error:
can't set flower variable COLOR to non-number blue error while flower 101 running SET
In NetLogo color, the names of the 14 main colors, plus black and white are defined as constants, so no quotes are required. Also, since they are constants, they are treated like literal values, so you can use them in the bracketed list notation, otherwise, you'd need to use the (list . . . ) reporter to create that list.
Also, your use of an array may be more complicated than needed.
You can write:
let colors [ red green blue yellow ]
set index random 3
let c item colors index
set color c
As an extra bonus, you can use the one-of primitive to do all the above:
set color one-of [ red green blue yellow ]
The accepted answer is the correct one, but as an aside, note that the read-from-string function will interpret a basic NetLogo color name as a color value:
observer> show read-from-string "red"
observer: 15
Also useful to know about is the base-colors built-in function that reports an array of the 14 basic NetLogo colors as numeric values, allowing you to do things such as:
ask turtles [ set color one-of base-colors ]
try setting your color names to number values, according to this site