PowerShell - .count() not working on an array of strings - arrays

I'm probably over thinking this, but I have an array of strings and when I enter $[arrayname].count(); I get an error that it failed because system.string doesn't contain a method named 'count'.
I know this works with an array of integers, and I would think there would be an easier way to get how many values are in an array other than doing a forloop.
Below is a simplified example. The example threw the error.
$Test=#("3","2","1")
$Test.count()

Lee_Dailey actually answered this question.
I needed to use the property [.count] not the method [.count()]

Related

Identify one particular field with RegEx

Apologies for the basic question but I'm new to regular expressions and am really struggling to find a solution to the problem I am facing.
I am trying to pull out a particular field from a json response dynamically, which can change each time I call it.
The response is:
[{"colorPartNumber":"10045112022164298","skuPartNumber":"0400218072057","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"0","label":"0","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["8"]}},"soldOut":true,"onlyOneLeft":false,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":false,"availableInPhysicalStore":false,"expectedShippingDate":null},{"colorPartNumber":"10045112022164298","skuPartNumber":"0400094632819","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"1","label":"1","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["10"]}},"soldOut":true,"onlyOneLeft":false,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":false,"availableInPhysicalStore":false,"expectedShippingDate":null},{"colorPartNumber":"10045112022164298","skuPartNumber":"0400218072040","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"2","label":"2","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["12"]}},"soldOut":true,"onlyOneLeft":false,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":false,"availableInPhysicalStore":false,"expectedShippingDate":null},{"colorPartNumber":"10045112022164298","skuPartNumber":"0400468014814","productColor":{"identifier":"Dark blue","label":"Dark blue","hex":"#0000A0"},"productSize":{"identifier":"3","label":"3","name":"Designer","scaleLabel":"apparel-wmn","schema":{"name":"UK","labels":["14"]}},"soldOut":false,"onlyOneLeft":true,"limitedAvailability":false,"preorder":false,"comingSoon":false,"visible":true,"displayable":true,"buyable":true,"availableInPhysicalStore":false,"expectedShippingDate":null}]
I am trying to pull out the skuPartNumber, but only when the "buyable" value is set to true.
Every thing I try I cannot seem to get just this one value :(
So in the example above the only value I want to pull out is 0400468014814.
This json is dynamic so there could be 100 values coming back, but the principle is the same.
One example of a failed attempt is skuPartNumber(.*?)"buyable":true, which only gives me the very first value (0400218072057), which is wrong.
Once again sorry for the basic question.
Try the following regular expression:
\"skuPartNumber\":\"(\d+)\"(?:[^}]*?\}){3}[^}]*?\"buyable\":true
Your answer is in the first match group.

Getting the first element in an array/any collection

So the other day I had my colleague review my code and he saw that I was using array[0], in Java terms this is basically getting the first element of the array. I did this several times for different purposes, all of which is to get the first element in an array/collection, for example list.get(0), to which he strongly disagreed with.
His argument was that somebody from non-programming background would have problem understanding it and using 0 in such cases is basically hard-coding, which is bad practice. I google-ed several times and all suggestions to getting the first element in an array or any collection is providing them the index, which is 0 in this case.
Could anyone provide me with a suggestion on getting the first element in a meaningful way?
Try using linkedlist's getfirst method to get the first element of the list.
If you were to use an ArrayList is backed by an array and hence its perfectly valid to use index as 0 to get first element.

Use Array of Values of Object in a Foreach Loop

I cannot seem to find anything about using the values of one property of an object in a foreach loop (without having the entire object placed into the loop).
I first create a function called UFGet-Servers that uses Get-ADComputer and returns the names of the servers in a specific OU in my environment and places them in an array. That's great, except that when I use the array in a foreach loop, each object that it grabs has #[Name=serverName] in it, which I cannot use in any useful manner. The following pseudo-code is an abbreviated example:
foreach($Computer in $ComputerNames){do code... code is adding the server name into a UNC path such as "\\$Computer\C$\"}
The problem with the above is that you can't add the whole object to a path -- it ends up looking like "\#[Name=serverNameHere]\C$\" which totally bombs out. How do I get rid of the "#[property=" part, and simply use the value as the $Computer in the loop?
What really weirds me out is that I can't find a straightforward article on this anywhere... would have thought everyone and their mom would have wanted to do something like this.
So, your issue isn't with ForEach loops, it is with string formatting. There are two ways that I know of to take care of what you need. First is going to be string formatting, which allows you to use {0}m {1} and so on to inject values into a string, providing that you follow the string with -f and a list of said values. Such as:
ForEach($Computer in $ComputerNames){
"The Server Path is \\{0}\Share$" -f $Computer.Name
}
The second way is a sub-expression (I'm sure somebody will correct me if I used the wrong term there). This one involves enclosing the variable and desired property (or a function, or whatever) inside $(). This will evaluate whatever is inside the parenthesis before evaluating the string. See my example:
ForEach($Computer in $ComputerNames){
"The Server Path is \\$($Computer.name)\Share$"
}

How compare two arrays of objects with assertArrayEquals

I want to compare two arrays of objects.
but there is not suitable method found for that method since it doesnt accept objects other from String, Integer etc..
I already override Equals method on the objects of the array.
But how do i pass the array to the method?
Assert.assertArrayEquals(esperado.getListaEquiposTorneo(), resultado.getListaEquiposTorneo());
//esperado.getListaEquiposTorneo(), resultado.getListaEquiposTorneo()) list 1 and 2 of objects made by me
First, you should be able to just use assertEquals
Assert.assertEquals(esperado.getListaEquiposTorneo(),
resultado.getListaEquiposTorneo());
I prefer to use Hamcrest as it gives better error messages
assertThat(actualArray,
IsArrayContainingInOrder.arrayContaining(
expectedArray));
assertThat(resultado.getListaEquiposTorneo(),
IsArrayContainingInOrder.arrayContaining(
esperado.getListaEquiposTorneo()));
IsArrayContainingInOrder
See Tomasz Nurkiewicz answer:
ArrayUtils.isEquals() from Apache Commons does exactly that. It also handles multi-dimensional arrays.
you can use a simple AssertTrue on the result

Mixing object types in a nested array

I'm pretty inexperienced with Actionscript 3, and I'm trying to make a nested array with different object types within it, but am having some trouble and wondered if anyone can help.
I want to create a nested array where the first element of the parent array is an array of movieclip instances that exist on the stage, but then the second and third elements of the parent array are arrays of string objects. This is what I'm trying:
var objectArray:Array = [ [instance1, instance2, instance3],
["word1", "word2", "word3"],
["word4", "word5", "word6"] ];
However I receive runtime errors such as this:
TypeError: Error #1034: Type Coercion failed: cannot convert "word1"
to flash.display.MovieClip.
As far as I'm aware, object types can be mixed in arrays, so I'm not sure what I'm doing wrong and endless searching online has not proved fruitful. Do any clever people out there have any idea what my problem is?
Thanks a lot :)
There's no issue with mixing types like you have done - the error you're getting is related to what you're trying to do to the objects in your arrays.
My guess is that you're iterating over the inner arrays using a for each loop that is typed incorrectly, for example:
for each(var i:MovieClip in objectArray[1])
{
// Error - objectArray[1] holds objects which are not MovieClip.
}
Or you might even just be trying to type cast the objects in a normal loop, which would give the same error:
for(var i:int = 0; i < objectArray[1].length; i++)
{
// Error.
var mc:MovieClip = MovieClip(objectArray[1][i]);
}
Mixing types in an array is generally not a good idea (one of the reasons being this scenario). If you update your question to explain why you're doing this, I can update my answer to explain a better way of going about it.
I managed to fix it. I had simply made a mistake in how I was indexing the array. Silly me!

Resources