PowerShell 7 Tutorials for Beginners #3 : Array & ArrayList

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hi and welcome to the third video in our Powershell 7.2 for beginners Series in the last video we took a look at variables how to initiate them and some of the different basic data types today we're going to be taking a look at two more different data types we're going to be looking at array and array lists and I'm actually going to pit them in a competition against each other to show you which ones actually perform better now you can definitely use the one that doesn't perform as good if there are some specific scenarios where you know if you're using very little amount of elements in these lists they really don't matter which one you use but once you get into some larger data sets we're going to see that one definitely outperforms the other so let's actually go ahead and let's just take a look at the array and array lists uh so once again we're just in Visual Studio code because we are using Powershell 7.2 so we're just going to click on file we're going to click on open folder and we're going to go to the folder where our Powershell scripts are so for me it's in my C drive in scripts and I'm going to be using this folder called free erase so we're going to be selecting that folder here and this is just going to open up this is probably the last time that I'm going to be showing this uh setup I think by now you guys should be pretty used to opening up a folder in Visual Studio code but here we are so we have our folder so we're just going to create our app dot PS1 here or actually I'm going to name it arrays dot PS1 all right and here we have it so like I said we're going to be taking a look at array and array lists uh now just a quick little overview is arrays are great if you're going to be dealing with something like under a hundred elements arrays probably would work but I would say that honestly any type of time you need a data collection like an array or an array list I would always go with the arraylist take the extra little bit of typing to create the arraylist instead of the array it'll great give you a great performance uh boost especially if you don't know the size of your data set 100 elements is very small you'll probably use you'll probably get larger data sets than 100 very easily but let's actually go ahead and let's just take a look at the arrays first so we're actually going to go ahead here and create our first variable so let's go ahead let's call this my array and to initiate an array you're going to make that equal to a at symbol open and close parentheses uh and that's basically it to create your array and then what we're actually going to notice here let me just see I'm just going to zoom in a little bit here that might help you guys uh so here we have our our my array and we have just made it equal to a at symbol and the open and close parentheses and then what we're actually going to do here is we're just going to show you the type so uh my array then we could do a get type and as we know get type is a method from this little Cube here we're going to do get type open and close parentheses we're just going to run this line and I need to actually create the array neural so it's not going to work uh so here we have it uh let me actually just zoom out here so my head is out of the way uh so here we have it it is a array as we can see it is an object and then the square brackets so let's go ahead and let's Zoom back in here and then what we can also see is if we do a my array we have a property called is fixed size so if we actually call that here and because it's a property and not a method we do not need these open and closing parentheses so if we run that here we get true and this means that the array is of fixed size now you're probably wondering if it's a fixed size how can I add or remove elements to it now what actually what Powershell does in the background when you add or remove elements to an array is it actually destroys the array and creates a new one with that new element inside of it and this is where that performance takes a huge huge hit because if you're constantly adding an element let's say you have 1 000 elements to add and you're adding them one by one you're adding your first element you're destroying that array adding the first element you're adding the second element well now you have to destroy that first array add the first element that you added in and then it'll add the second element so as you can see as you're adding more and more elements the performance actually degrades drastically uh to where if you have very large data sets you're taking a big hit uh in performance and we're actually going to see that in this video um so let's actually just see how we can add um some items here so let's actually start our array uh with a couple items here so my array we are going to put uh test one here we're gonna put uh test two and we're gonna put final item here we're gonna put tests three all right and we're just gonna remove these here so we have our array and we could do a my array dot count so if we do a DOT count as we can see there are three elements in our array we also have the my array dot you'll see dot length here so if we run this once again we see that we get free and free length and count will actually give you the exact same numbers so now let's say if we wanted to display our entire array the way that you would do that similarly to how we displayed our variables we would just put our variable name in here so as we can see we get test one test two and test three Now list arrays as the same thing was arrays in other programming languages we can actually display specific items from our array using an index and square bracket so we're going to do my array and then we could do a square bracket square bracket now the only thing is with an index it is a zero based index now what that means it actually means that this test one is at index 0 Test 2 is at index one and test three is at index two so if we actually want to print out test two we'll actually have to put element one so if we actually just run this here we will see if we get test two and if we do a element two we get to test free now let's see if we try test uh my element brief we get nothing back and that's because the element doesn't exist now we actually did see in the last video a strict mode option now if you had your strict mode um to the version latest up here let's set that up and let's see my array free we actually get an error and it says that the index was outside of the bound of the array so if you are programming something um and maybe you're just constantly adding items or you don't have the checks now we're going to be seeing ways that if you're looping through an array I know we don't know what a loop is yet but basically we're going to be iterating through each item in the array you don't necessarily have to set the strict mode on the amount of times that you would actually go out of bounds there are a lot of ways to avoid going out of bounds in the array but if you are manually typing in values in here because you're expecting certain things to be there setting the strict mode can cause errors so definitely use that strict mode as I said before it's very much up to you to use it if you want to use it um of course there are situations where your work might require or you have strict mode on to ensure data Integrity maybe um or that's just the common practice but that's just to show you guys what would strict mode cause if you do go out of bounds it does give you an error but now that we set it back to off if we look at Brie we just get null so you can definitely program to see if it's equal to null maybe there is a null inside of your array but that could be a way to check for out of bounds but also we do have the count so you can always make sure that you're always within those numbers uh so now we need to add and remove items from this array now you're going to see if we actually try to do the my array and then we do a DOT we actually see this function here called at now the majority of people would click on this thinking that we can add an item here so if we do test four and we try to run this ad we actually get an error we get an error exception calling ad with one argument collection was of fixed size um you can see that error but is a fixed size um does actually appear at the end here where my head is um so that is definitely a problem um because now how do we add if the add method does not work so the way that you would add to an array so there are two ways that you can add an item to an array the first way would be my array equals my array Plus test for now if we actually print out my array here we're just going to remove this line so we print out the Maya rate twice here so we have the the first execution of the my array at the top here and then the second one after we add test four and if we actually go ahead and just look at it one more time here so we have all four elements if we look at the count now we will see that we actually have four now the other way to add an item to the array is actually to do the my array plus equals the element you want to add so we're going to do test five and then we're going to go ahead and print out the array here so once again we see that we have elements one two three four five and if we do a DOT count we will also see that we get five elements so as we can see um we do we are able to add elements to it without a problem now we can also remove elements now you will notice that once again the my array will actually have a remove and a remove at Option now the remove at will remove at a index so if we do the index of one which in various should remove this test too we will once again see that we get an error because the collection was a fixed size so now intuitively you might think that we could just do a subtraction to remove the item unfortunately that's not how it works but we can remove items out of an array by doing a my array equals my array space dash n e and then a quote quote let's remove test two here and now we can do a my array just to display it out here so there we have it once we run it we get test one test free test four and test five so what this really does is it takes it sets up your my array to be equal to the my array where it is not equal to test two so any instances of test two it would get removed so if you did have multiple test twos in here those would also get removed so it might not necessarily be exactly what you want to do but that is kind of the limitation with the arrays the removals are very very uh odd at best you can't remove at a specific index and if you do the not equal it'll just remove all the instances of that of that text or the item that you're trying to remove so now let's actually go ahead and let's take a look at the array lists now the array lists are in my opinion much much better than a race if I ever have if I'm ever writing a script where I actually do need an array I will always use an array list uh just for that performance boost I know that in the back end it works a lot better I might know that when I'm first creating um the script maybe there's only three elements in the array I'll still use an array list just in case that number Grows Where I don't really know how big it can get but I know that it can get bigger so why not do an array list in that situation so let's actually go ahead and let's see how we can create our array lists here so there are actually two ways that we can create a list so let's do a my list one and we're going to make that equal to a square bracket square bracket and then inside these square brackets we're going to do system dot collections Dot arraylist and then our at symbol open and close parentheses after the square bracket that is Method number one the method number two which is actually going to be my recommended method now I know on a lot of videos you're actually going to see me do this this was actually kind of a bad practice on my part uh for a while I'm trying to kick that habit which is why I'm making these videos once again um and just kind of going over some of the uh better practices here so it we're going to create a new object and we're going to do a type name here and we're going to do the type name of system.collections.arraylist and here we have the money list one and my list so if we actually go ahead and create both of these and we just look at the my list one dot get type and the my list dot get type and let me actually just zoom out here so you guys can actually see what will happen so the my list one is a array list it's a system object and the my list is also an arraylist and it is an object so they are the exact same item but the main differences between the two is this one will actually cast the array list to this array so it's an added it's an added task in the background whereas the new object will strictly just create a new item of arraylist type so those are really the those little differences um now I would recommend using the new object instead of the old method of you guys used to see me do which is like with the square brackets and then the at symbol in the parentheses but both of them work I believe at the end of the day um the performance difference between the two unless you're creating thousands and thousands of arraylist probably very very minimal just going from an array to an array list will greatly make up the time loss so I definitely wouldn't worry about it too too much but just be aware that you can actually create an arraylist simply by doing this instead of doing the casting of an array so once we have our array list here we can also look at the my list dot is fixed size so if we look at is fixed size we will actually see that now it is false so that's neat the array was fixed size but the arraylist is not a fixed size so what we can actually do here is actually do the dot add and we can add one here and we will see that we get an output here you're going to see that says zero and then if we actually go ahead and we do this again we get one now you might be wondering what is this output that we're getting when we're just adding items this is actually the index that gets Associated to the item that you're adding so we're adding at index 0 for the first item the second item that we're adding is going to be at index one now you can actually nullify this output to basically tell Powershell to not output this by simply putting a square bracket in front and putting the words void inside so if we do a void here and we actually just re-run these items as you can see we get no output from them and we get now we get the my list and the items are still there so that is how you can add items and you can add items without having an output to the screen to say what index they are now another neat thing that you can do with arraylists is actually a my list dot we do an ad here we get add range now what an ad range is you can actually add a array of items so we here we can actually ADD test free we can add that or and let's add S5 here and let's then print out my list so if we run all of this here we will see that we have one two three four and five and if we do the dot here we have a DOT count now you're going to notice that we actually do not have a DOT length anymore we only have the dot count um and here we have it we have five elements which makes sense because we have our one two three four and five now the same thing as arrays or array lists if you do a my list um at index one here we will see that we get test two which makes sense and as we kind of saw with the add function was the output of the index this would make sense now compared to the array the arraylist has a very easy and intuitive removal method so we can do a my list Dot so we have removed we have removed at and we have remove range so we can remove uh now this will remove a specific item here so if we actually remove test two and then let's go ahead and let's print out the my list here at the end so as you can see we do not have a test to two anymore and if we run that again we don't get anything back nothing really changes because there is no more tests too now let's see if we actually added a second two in here and we ran this now as you can see the remove Test 2 actually will only remove the first instance of that element in the list so that is something to keep in mind with the remove method it will only remove the first instance of that element so test one test two test three test four test five test two once it hits the remove it will go in order so it'll look at index 0 1 2 3 4 5 Etc and the first time that it actually hits that value of test two it will remove it and that's where it stops it does not keep going and then we also have the remove app here which will remove at a specific index so if we actually remove that index zero we know index zero will exist that will actually removed test one so then if we look at the my list afterwards run all of these again now we're left with test spree four five n two and then we of course have the uh remove range uh which this you would give it the starting index and then the number of times that you the number of items you want to remove so here we're going to start at index 0 So in theory index is zero now at this point we know it's going to be test three and we're going to be removing two items here so let's see what happens we actually run this entire line here give me one second just doesn't seem to want you can cooperate with me there we have it all right so here we have it we have test five and test two now at the end and that is because we started just bring this up a little bit here so we have Test free test four and we started at zero so we have this one and we tell it to remove two elements so we have one and two and it leaves us with test five and test two so those are all the different types of remove methods that you can do with the array lists and the different types of ads that you can do so as you can already tell the arraylists are definitely a lot easier to use and kind of more intuitive but let's actually look at the performance differences between the two so I'm going to be showing you guys a new commandlet here uh now you guys don't actually have to really know this commandlet for now uh this is a little bit more of an advanced commandlet um I I will have a quick tip on this commandlet as well uh coming out very shortly but it is the measure command command list now what this is is you pass in an expression and it will actually calculate how long it takes uh in the matter of time how long it takes to execute that command or that expression so here we'll just do measure command Dash expression and we're just going to make sure that we actually go ahead and we are going to create an array and we're going to create an array list here so let's create array and let's go ahead and let's create array list and we're going to create it using again the a better method here so we're going to do the type name system dot elections Dot arraylist all right so then in our expression here what we're going to type is we're going to do a at symbol open and close parentheses and inside those parentheses we're going to do a zero dot dot 50 000. and then after the parentheses we're going to do a four each open and close parentheses and inside of those we're going to do an open and close curly bracket and inside of that we're going to do array plus equals dollar sign underscore and this will add the item here into the array so let's actually just run the array first here so we're going to run this command we're going to create the array and run the measure command so I'm actually going to run this now I'm actually going to pause the recording we're going to come back when it's actually done because it does take a little bit of time so I will see you guys in a few seconds all right so we are back here uh so it just finished it took a pretty long bit of time um so here we will actually just expand this here and we actually see that it took for the four each here it took one minute 17 seconds um and then the text here as well but basically one minute and 17 seconds for the array to actually iterate through 50 000 elements and add it to the array so let's actually go ahead and let's take a look at the same here but with the array list so we're going to put this here and then we're going to say um for each we're going to do array list dot add and we are going to add the dollar sign underscore and let's go ahead and let's run this and now I won't actually even pause this because it'll be done very very quickly so and here we have it it's already done uh so you guys can already tell how much faster it is uh so this actually did not even take a minute did not even take a second it took 195 milliseconds uh so in comparison to a minute and 17 seconds uh this is definitely much much faster um there's not really a comparison at this point uh and there's actually a way to do this even faster with the array lists uh with actually what we saw because as you can see here we are taking an array of 50 000 elements and we're saying for each item in the array um which this you will all learn a little bit later on this is a little bit Advanced for this video but I just wanted to show you guys the performance differences between the two um so here we're grabbing an array of 50 000 we're actually iterating through each item of that array um and adding it in to the array list um and then here we're adding it into the array but what we actually saw is that we could do an array list add range function so let's actually do that in this one here so what we're going to do here is array list dot add range and we are going to add the array of 50 000 elements and if we run this here we will see that this only took 18 milliseconds so once again it shaved off another 180 milliseconds uh so it's basically taking almost no time at all at this point I mean the difference between the two like if you already have like a set array like you already have like a set amount of numbers that you want to add to an array list uh the ad range is definitely better the amount of times that I think I've been able to use at a range in certain situations have been a little bit limited uh usually you get fed things per item uh in which case uh the dot add is definitely a little bit more comparable um but even with this you guys saw we went from a minute and 17 seconds to under one second it was just under 200 milliseconds with the array list um and that was 50 000 items now that is a pretty large data set I wanted to pick a large data sets just so the difference would be very uh noticeable now of course the smaller data sets the difference will be much smaller but that being said I would still recommend as soon as you hit any type of array just make an array list this way if your data set ever grows you will get that benefit from the arraylist you won't have to worry about it you won't have to worry if your data set grows to a million items uh in a couple weeks why is your script taking a double triple quadruple the amount of time that it should um it could just be because you're using an array instead of an array list and that's where your slowdowns are happening because as you can tell if we kept doing this for millions of items like this is only 50 000 we're already taking an extra minute um so you can imagine that if we're at a hundred thousand you're talking about an extra two three minutes if you're talking about a million you could be talking your script taking about 10 to 15 minutes longer and that's if you're only using one array if you're doing multiple array lists and multiple arrays that time like you're drastic drastically just increases um so I would definitely recommend an arraylist versus an array and uh there's the a way to also measure a command now I will actually be releasing a quick tip on measure command I'll go a little bit more in depth on it but for this video really the only takeaways are the arrays and the array lists in the next video we're going to be taking a look at hash table and some custom objects as well so be sure to stay tuned for that hit the Subscribe button hit that like button and also be sure to hit that notification Bell to be notified when that next video comes out and as always if you guys have any questions or comments please let me know Down Below in the comment section if it's something specific I'll just answer you directly but if it is something that a lot of people can benefit from I will definitely be making a video on it and I will see you guys on the next video
Info
Channel: JackedProgrammer
Views: 18,142
Rating: undefined out of 5
Keywords: powershell 7, windows powershell, configure vs code for powershell, visual studio code powershell, programming, coding, scripting, how to install powershell 7 on windows 11, powershell, powershell for beginners, how to start with powershell, scripts, powershell fundamentals, powershell 7.2, powershell 7 windows 11, windows 11, how to, beginners, powershell basics, powershell tutorial, powershell scripting, array in powershell, arrays, arraylist in powershell, array vs arraylist
Id: kpYCCs_sgW4
Channel Id: undefined
Length: 31min 37sec (1897 seconds)
Published: Thu Sep 29 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.