Understanding $args in PowerShell
-
@chutestrate said:
Sorry martin9700 I'm not even close to understanding what you are trying demonstrate.
Martin is correct, that trying to tackle $args when you don't have a solid understanding of everyday arrays first is going to be confusing.
-
@chutestrate said:
Sorry martin9700 I'm not even close to understanding what you are trying demonstrate.
LOL, which is Scott's point.
I'm not a fan of using $Args because I always preach (and you can check the PowerShell forums over at Spiceworks to back this up) that I always write scripts for the next guy and using $Args in code make it very hard to decipher what the original writer is trying to do. Named parameters are self documenting (assuming you are using meaningful variable names). But to Scott's point, they are a more advanced technique.
-
Oh, I have an idea.
Think of my way of doing $args as being like MadLibs. But you just give a string of words and they get put in in order. Your program or script is like a MadLibs where you feed it the list of words and it says funny things. In fact, making an actual MadLibs script is a common and fun scripting project to do for exactly this reasons.
Now Martin's way is more like MadLibs where you know some basics like name, age, color, size, etc. and you put in words associated with those things so that you get a simpler matching of words to where they are used.
The approach that I am demonstrating is just taking things in order one after another. His is to identify what they are and label them so that they are more useful. Both are important to understanding, but one after another. In the real world of script writing his approach is way, way more common and practical. Not every single time, but the vast majority. But understanding my way first is important because you need to know what is happening.
-
@scottalanmiller said:
for ($i=0; $i -lt $args.length; $i++) {
'This is $args[' + $i + "], which is: " + $args[$i]
}This could also be written:
for ($i=0; $i -lt $args.length; $i++) { "This is `$args[$i], which is: $($args[$i])" }
-
That's a little better I'm a BASH, Python and Ruby guy, I don't really write PowerShell normally
-
You guys are right about needing to understand arrays better i guess. I just started this last week, and thought i had at least a slight understanding of it. I can follow the scripting commands in the first line, but what is the second line doing?
At least i can make ps1 files, and get it into powershell, lol
-
@chutestrate said:
I can follow the scripting commands in the first line, but what is the second line doing?
The second line is just printing out the name of the argument that we are on and what the value of that argument is for you to see. When you look at the output you can see it patch up. You get to see the name of the argument like $args[0] and the value held by that argument.
-
An array, as we discussed via PM, is an "array of things", like a list. As we are programming here, that is really a list of variables. So in the same way that a normal variable can have something in it such as a string, so can an individual array element. Here is an example:
$myname = "John"
or as a single element array:
$myarray[0] = "John"
These two things accomplish basically the same thing. You have stored your name in a variable. Now what if we want to store a second name?
$yourname = "Peter"
or as part of the array that you already made:
$myarray[1] = "Peter"
Again, we did the same thing. But to work with the plain variables we need to know two variable names. To do the same thing with the array we need only know one array name. Doesn't make much difference when we have only two. But what if we had ten, or a hundred, or an indefinite number of names to store? Then an array because way, way easier to manage because it can be of arbitrary size holding just one name or thousands.
Each has its place, but arrays are a very important way to hold data in a program.
-
So think of an array as a list of variables that don't have their own names. They are just "first array element", "second array element", "third array element" and so forth until the list is complete. If you don't know how many array elements you have it is easy to do a for loop through them...
foreach thing in my array do something
That's great because you need only know the array name. The foreach loop figures out how many things are in the array and does something with each one. If you had hundreds or thousands of individual variables that you wanted to do the same thing with, that would be a mess.
-
That makes sense.
-
For my current limited understanding arrays are easier to understand than than the $args
-
@chutestrate said:
For my current limited understanding arrays are easier to understand than than the $args
$args is just an array, nothing more.
-
Here is a little array program to do basically the same thing as above but using an array defined inside of the script rather than passing in arguments from the command line...
$myarray = "John", "Peter", "Linda", "Scott", "Norman", "Jenny" for ($i=0; $i -lt $myarray.length; $i++) { 'This is $myarray[' + $i + "], which is: " + $myarray[$i] }
-
When you run it you get this...
> .\arraydemo.ps1 This is $myarray[0], which is: John This is $myarray[1], which is: Peter This is $myarray[2], which is: Linda This is $myarray[3], which is: Scott This is $myarray[4], which is: Norman This is $myarray[5], which is: Jenny
-
@scottalanmiller said:
@chutestrate said:
For my current limited understanding arrays are easier to understand than than the $args
$args is just an array, nothing more.
don't follow that one.
-
So as you can see, I set the array to have those names in it with this line...
$myarray = "John", "Peter", "Linda", "Scott", "Norman", "Jenny"
When you use $args, the exact same thing is happening except that PowerShell is doing it automatically and hidden from you because it is happening from the command line and you don't write it out explicitly. But what is happening under the hood in a case where you do this...
.\myexample John Peter Linda Scott Norman Jenny
What PowerShell is doing without you having to write it is basically this...
$args = "John", "Peter", "Linda", "Scott", "Norman", "Jenny"
-
@chutestrate said:
$args is just an array, nothing more.
don't follow that one.
Not sure what else to say. It's that simple. $args is an array. $myarray is an array. There's nothing more to it. The only thing that makes $args special is that it is the one array that PowerShell makes for you when you run your script.
-
Not a problem. This has been phenomenal. I've learned a lot. I'll try to use this to understand the args. Unfortunately, I'm looping back to some of my original confusion. I know you addressed it, but it's not making completed sense.
-
Definitely leave $args for now and focus on arrays. Once you are solid on arrays, $args is pretty much self explanatory. Try doing some sample scripting with arrays for things. That will help a lot. In all seriousness, doing a MadLibs script is a good way to learn.
-
I'll do that.