Understanding $args in PowerShell
-
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.
-
I doubt that Martin will agree but I don't find PowerShell to be particularly well suited to learning programming concepts. It's a great language for what it is, but I'm very glad that I learned programming on other languages and then learning PowerShell.
-
I'm not fooling myself. I don't do well with trying to be a programmer. This is part of the testing goals. I just got interested in powershell, and am trying it out. Thank you for all the time. I'm rereading the posts, and I'm going to have a hard time matching $args to arrays. Maybe tomorrow it will click.
-
@chutestrate said:
I'm rereading the posts, and I'm going to have a hard time matching $args to arrays. Maybe tomorrow it will click.
Just remember that there is nothing to match. $args is just another array like any other. It's not a different thing. It's not "like" an array, or similar to or anything. It is an array. $args is the name of an array. I have a feeling that you are thinking of $args as a thing rather than the name of an array.
-
I definitely am. Unfortunately, i don't see it the way you are telling me.
-
@scottalanmiller said:
I doubt that Martin will agree but I don't find PowerShell to be particularly well suited to learning programming concepts. It's a great language for what it is, but I'm very glad that I learned programming on other languages and then learning PowerShell.
The languages I've mostly worked with are batch/cmd, Visual Basic, vbScript and PowerShell. So PowerShell is a vast improvement over those
It took me awhile to really grasp the idea of objects and using them for everything in PowerShell, but once I did the whole language opened up to me and I began to realize all the things I could do.
-
Both of you have been immensely helpful. I hope I can return the favor in some way.
-
@Martin9700 said:
@scottalanmiller said:
I doubt that Martin will agree but I don't find PowerShell to be particularly well suited to learning programming concepts. It's a great language for what it is, but I'm very glad that I learned programming on other languages and then learning PowerShell.
The languages I've mostly worked with are batch/cmd, Visual Basic, vbScript and PowerShell. So PowerShell is a vast improvement over those
It took me awhile to really grasp the idea of objects and using them for everything in PowerShell, but once I did the whole language opened up to me and I began to realize all the things I could do.
I come at it from the other direction. Was doing C and Fortran, then Java and C#. Everything is better than Fortran, though.
-
@chutestrate said:
I definitely am. Unfortunately, i don't see it the way you are telling me.
If I could figure out how you are perceiving it then maybe I could help more. I'm not sure how you are looking at it, though, which makes it hard for me.
-
Trying to sort it out. The automation piece is part of it. If it's created automatically why is the programmer designating what $args[0], [1], [2], etc is.
-
@chutestrate said:
Trying to sort it out. The automation piece is part of it. If it's created automatically why is the programmer designating what $args[0], [1], [2], etc is.
The programmer is not designating what they are. The programmer is reading the contents out. At no point, in any example, did we specify in our program what the values of the elements in the $args array were. We never did that. All we ever did was print out their contents to show what they had been set to by PowerShell.
-
Let me take a stab at explaining an array. It's just a list. When you write your groceries on a piece of paper you have a variable with multiple values on it. You don't write a single item per piece of paper, right? If you did that'd be a regular variable. Instead you put the whole list on one piece of paper, making it an array/list.
Computers are great with lists, but they have some limitations, and they have to be told to do things in a very literal sense. So, grocery list is:
$GroceryList = "beer","wine","whiskey","brandy","vodka","chips"
In PowerShell, the comma's are what tell PowerShell that this is an array. Now, if I ask you what's the 3rd item in the list, you would scan through and count each one, until you got to "whiskey". PowerShell is the same way, but we can jump straight to the third item. Try:
$GroceryList[3]
The brackets with the number tell PowerShell that you want to reference a particular item in the array. The number tells you which item. So we got Whiskey, right? If you tried it, you know we got "brandy". WTF? In programming languages, and this is pretty much universal, arrays always start at zero. So $GrocyerList[0] is beer, $GroceryList[1] is wine, and last $GroceryList[2] is whiskey. So when working with arrays you'd really want to:
$GroceryList[2]
To have PowerShell return the 3rd element. Each item in an array is called an element, by the way. Looking back at Scott's code, what he was doing was creating a For/Next loop, which is a loop using a number as reference.
for ($i = 0; $i -lt $GroceryList.Count; $i ++)
Is some goofy looking stuff, but the first part $i = 0 marks our starting point. $i = 0. The next part is a IF statement, that as long as it's true we'll continue the loop. In PowerShell we have built in properties, and one of those is Count which tells us the number of elements in the array. So as long as our $i variable is less then that count, we'll keep looping in the for loop. The last part $i ++ tells the loop, that when I'm done with the loop add one. The ++ thing is basically shorthand for $i = $i + 1.
Now PowerShell will continue to run the code contained in the scriptblock (all the code between the curly brackets) until our $i variable equals or is greater than the number of elements in our $GroceryList. So the first time through the loop $i = 0, we display that number, and we reference the element in our array that corresponds with zero. Which is beer. Loop ends, $i increments to 1, start again.
Loop displays the number 1, and shows the $GroceryList element that corresponds to that number, which happens to be "wine". Code block ends, $i increments to 2. This is still less the $GroceryList.Count (which is 6, btw) so the loop repeats. Onward until $i get's to 6, at which point it exits the loop and continues on with the script. In this case there is no more script, so it exits the script and drops you back into the shell.
-
I'm actually following that. But the $args to array thing....
This isn't stating that $args is?
for ($i=0; $i -lt $args.length; $i++) {
"This is `$args[$i], which is: $($args[$i])"
}