Understanding $args in PowerShell
-
-
Rob thank you for doing that. I've watched it a few times, and I'm not catching what you are trying to demonstrate. I'm sorry.
-
I need to correct my last post. I understand the named parameter demo, but not the $args piece of it.
-
Are you able to describe in any way how you are finding $args confusing? I realize that when you don't understand something that there is really little way to describe how it is being misunderstood. If you knew what you didn't understand... you'd understand it. But I'm trying to come up with some way to figure out what is being missed. If you know what an array is, you know $args. So I'm wondering if arrays aren't understood or if you just aren't accepting that $args is just the name of an array or what.
-
Ok, I'm kinda with you at this point. Correct me if anything I'm thinking is incorrect. I'm using english, not the correct terms.
I understand that $args is automatic. I believe the mental block is the where it is an array, or is it creating and naming an array [$args[0], $args[1], etc.) Wait, you told me that the numbering of $args is how powershell is matching the automatic creation of $args to an array? So each auto creation of $args is an array?
I believe I'm ok with the concept of explicitly creating and naming an array.
What I'm having the heartburn over is how $args is an array, or creates an array, or was just put there to drive me a little crazy. Do you have to put it in the code you are developing? I've been given some code examples that included it. Were these just examples of what would look like or is it necessary to be used if not creating/naming an array. If it is automatic how do you use it later on?
Hopefully, these questions give some kind of hint to my confusion.
Thanks again for all the support.
-
sigh. I just don't think this is going to happen. I think it's been explained as many ways as possible, and I keep missing the message.
I think it's time to let this go.
The support has been terrific thank you.
-
@chutestrate said:
Wait, you told me that the numbering of $args is how powershell is matching the automatic creation of $args to an array? So each auto creation of $args is an array?There is an array. That array's name is $args. That is all.
Yes, each program gets one $args and it is always an array.
-
@chutestrate said:
What I'm having the heartburn over is how $args is an array.
This is where you are stuck. $args is an array. That is all that there is to it. It just is.
-
@chutestrate said:
Do you have to put it in the code you are developing?
No, not if you don't want to use it. You never have to use anything that you don't want to.
-
@chutestrate said:
If it is automatic how do you use it later on?
No idea what you mean. It's an array. Use it whenever you want. It isn't like arrays go bad after a period of time or something. It's just an array. As long as you know what an array is and that this is an array, that should explain everything. It's just an array. It's not required, it's not magic, it doesn't time out. It is absolutely exactly like every other array.
-
I think you are right. I'm stuck between $args and arrays. Fine, $args is an array to use or not use. Then $args[0] and $args[1] are different arrays?
-
nm, i think i get it now
-
@chutestrate said:
I think you are right. I'm stuck between $args and arrays. Fine, $args is an array to use or not use. Then $args[0] and $args[1] are different arrays?
No, in any array the array is named like this:
- $args
- $myarray
- $shoes
The use of [0], [1], etc. is to refer to specific positions within the array. So $args[0] is the first variable in the array. $args[1] is the second variable in the array and so forth.
-
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