read elements of an array simultaneously

Hi All,
I am a newbie to PS (though have worked on other languages). I am looking as to how do I read array elements simultaneously without looping through it. I have written a foreach loop that iterates through an array, but how to get all elements without iterating

You might show some of your code to clarify what you mean. (format code as code please)

Ditto to what Olaf says here, as based you your direct post, the way you are phrasing it, you can’t. Yet, I may be interpreting what you are asking incorrectly. Sure, in PS< you can do a ReadAll kind of things, but you are still going to have to iterate of that in memory load, vs addressing items as they are read in.

Any see if the below gives you an guidance before you post back to us.

Powershell: Everything you wanted to know about arrays https://powershellexplained.com/2018-10-15-Powershell-arrays-Everything-you-wanted-to-know

Good to know you are already coming from another language, and that should help you ramp up foster, but you should still spend som time rammping up on PS.
There is a lot to it, and not ramping up will lead to unnecessary confusion, misconceptions, frustration and errors. Yet, again, it is a different language.
Don’t expect it to act like what you know, syntactically or otherwise. There are many way sto do the same thing in PS natively and if you decide to leverage the .Net libraries/namespaces and C#.

See these resources.

https://www.reddit.com/r/PowerShell/comments/ar6cvt/powershell_in_depth_second_edition/egmlpom/?context=3

https://www.reddit.com/r/PowerShell/comments/afqmmw/i_want_to_help_my_husband_advance_his_powershell/ee3k6p6/?context=3

And this…

Best Practices

Windows PowerShell
https://docs.microsoft.com/en-us/powershell/developer/windows-powershell

PowerShell Standard Library: Build single module that works across Windows PowerShell and PowerShell Core

Windows PowerShell Best Practices

PowerShellPracticeAndStyle

PowerShell scripting best practices

Powershell - Recommended coding style

What is the recommended coding style for PowerShell?

PowerShell 4.0 Best Practices scripts
This is a place holder for all the scripts from my forthcoming Windows PowerShell 4.0 Best Practices book. Once the book releases, I will upload the 200 scripts to this location – which is currently referenced in my book. The idea is similiar to the one that I did for the Wi

Thanks All for your reply and thank you postanote for your reference guide pointer. Here is a sample code

$testarray = ("a", "b", "c", "d")

foreach ($x in $testarray) {

print $x

}


This loop actually iterates this array and prints

a

b

c

d

Now I wanted to perform it in a way that this print statement is executed on all elements at once and so that result is like

a b c d

Hope I am clearer this time

$testarray = (“a”, “b”, “c”, “d”)

“$testarray”

@kvprasoon $testarray will print array but will not help me do a simultaneous operation on all elements of an array. What is wrote is just sample where if I replace print statement with anything else it should work on all elements simultaneously

You could try something with background jobs, but still you will have to loop, but you can act on second element without waiting for first one to finish.

I suggest using PoshRSJob module for this.

powershell seems to take the semantic approach, it’s not fortran or assembly language for real time process control but its syntax allows for doing exactly what you request in an powerful object oriented way, you just can’t have a cake and eat it too, unless you get a bigger cake :slight_smile: