Understanding $args in PowerShell
-
so, arrays within arrays?
-
@chutestrate said:
so, arrays within arrays?
Not necessarily, just the value of the $args array at position 0.
-
not following.
-
@chutestrate said:
so, arrays within arrays?
NO.
$args[0] is the first item in the $args array.
$myarray[0] is the first item in the $myarray array.
$shoes[0] is the first item in the $shoes array.They are all just an array. $args is simply an array. nothing more nothing less.
-
An array is just an unordered list of objects. To call from an array in powershell you need to identify the array, in this case we use the identifier $args, and the location of the object you are trying to get, this can be anything like [0],[1].[2],[n] (n being a variable for any number).
-
I'm trying to start to use the term 'item' or 'element' when referring to the things contained in an array (in general). In Powershell, you can start to refer to these things as 'objects,' but within the terminology of array, I'm going to try to use 'item' or 'element' for the sake of consistency.
...in short, lots of terms get thrown around interchangeably.
Single dimensional arrays are a grouping of single elements - like an egg carton, each spot in the array can hold one thing. This is what $args is.
Multi dimensional arrays are a grouping of groupings of items - like a shipping container. A large box that contains smaller boxes of things.Watch this video up to 2:05 to get a basic understanding of arrays - - it sounds like you don't grasp this concept at all
-
Think of it like this:
You buy cars over the course of your lifetime. We are going to mentally think of all the cars you've ever bought as an array called $CarsIveHad
$CarsIveHad has no items inside. It is a null array. You can't do much with it at this point. This would be like me saying $PorschesIveHad. I'm fairly certain this will remain null for the rest of my life, but that's not what we're talking about.
---Setting up your array:---
You buy your first car, it's a Yugo.
Now $CarsIveHad has 1 item inside. We start at number 0, because that's what we do in programming/scripting/computer stuff.
$CarsIveHad[0] = Yugo
You buy your next car 3 months later, because Yugos suck. You buy a Chevrolet Cavalier.
Now $CarsIveHad has 2 items inside.
$CarsIveHad[1] = Cavalier
5 years later, you upgrade to a GMC Terrain.
$CarsIveHad now has 3 items inside.
$CarsIveHad[2] = Terrain
Or, a family:
$BradyFamily[0] = Mike
$BradyFamily[1] = Carol
$BradyFamily[2] = Greg
$BradyFamily[3] = Marsha
$BradyFamily[4] = Peter
$BradyFamily[5] = Jan
$BradyFamily[6] = Bobby
$BradyFamily[7] = CindyAll together, they are $BradyFamily
-
I guess not.
-
@chutestrate said:
so, arrays within arrays?
No, not at all. I'm just talking about straight arrays. An array is a list of things. $myarray is the name of the list. $myarray[0] is the name of the first item in the list.
-
Thank you
-
So unless other arrays are created $args would be the only array with many elements contained within it.
-
@chutestrate said:
So unless other arrays are created $args would be the only array with many elements contained within it.
You're overcomplicating this - There's no 'unless' here. I'm not sure where you got this from what we posted. Other "things" have no bearing on the state or existence of $args. $args is always an array. Before you run a script or function, it does not exist. Then, it is generated every single time you execute a script or function within the confines of that script or function. When the script/function ends, the $args array is destroyed.
It could be full of elements, it could be empty. It all depends how the script or function is executed. Did arguments get passed to the script when it was ran? Yes? Then $args has elements contained within it. No? Then $args is a null (empty) array. Like my example of an egg carton. It could be empty, but it still exists.
The thing to remember here while learning this is to empty your mind and read our posts at face value. We are going to tell you only the things you need to know in order to understand the concept. When you start making assumptions and leaps about things we didn't mention is really where you are getting into trouble.
-
Rob I do try to take things at face value. I went back over what has been said and haven't found where you or anyone has said that it's created each time a scipt is run. Very likely I misunderstood something. My assumptions are only testing what I think so I know whether or not I get it. Since I now know it's created new each time something is run that makes a big difference to my understanding. It's fine to tell me to take your posts at face value but if I'm left with a question about it the face value is not a value. I'm not trying to be disrespectful however what is plain to you isn't to me. So Merry Christmas and thank you for the excellent discussion