Starting out with Powershell in the multiverse! Multi Dimensional Arrays

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello again everyone today we're going to talk about more Powershell but we're going to talk about Powershell arrays and we're going to talk about partial data storage so that's going to include things like arrays and hash tables and look a bit about Pierce objects we'll also look at multi-dimensional arrays so we'll look at 1D 2D and 3D arrays that we can do within powershop so I hope you enjoy this content and let's get straight into the demos foreign [Music] okay then so we're here up in vs code again and we're going to start looking first of all at arrays but before we do that we need to go and check out first of all uh variables again so if we do something like dollar VAR equals let's do Apple inside here and go and run this code and then go and call this dollar VAR down here you'll notice it just returns Apple we could actually run something else on here called dot get type with a couple of colors on the end of it and you can see that this is actually a type of string and it's actually got a base type of system Dot dot object now what about if we want to add multiple things to this powershell's quite nice with data manipulation in this sense when it comes to scripting it's quite easy we don't have to declare types if we don't want to that can cause problems later on but we'll get into that in another video and if we want to actually make this a variable we can very easily just add more things to this apple berry and another comma down there for pair and we'll just go and run that line again with f8 and we'll go and call that dollar bar now you can see we've got apple berry and pear now what about if we just zoom in here a little bit what about if we do that same command dollar VAR dot get type now now we can see that this is such an object with those two square brackets this allows us to see that this is actually in Array now there is other ways of declaring arrays for example we can actually create something called a blank array as well so if we just comment out that code at the top here and if we do dollar fruits okay we put an equals on there at time and then two uh brackets like that and run this this here is declaring that this is actually an array it's an empty completely blank array we can add data to it later on if we want to so if I go and now look at Dollar fruits in my terminal down here and look at get type we'll go and run that you'll see this again is an object but if I go and run dollar fruits to go and see if there's anything inside here there is nothing inside here at the moment now if I want to add more things to these fruits I can do if I really really want to um I can use the special little operator to do this so I can do dollar fruits up here and do plus equals this is where the magic happens a little plus equal sign this is this just add something to this array so we can add to these fruits Apple for example now if I go and run that line and now I go and call the variable of dollar fruits to see what's inside it you'll see that I've got apple let's go run that down here in the Shell as well let's make a bit of room and just do clear on that just so we can bounce this back up to the top now if I do dollar fruits again and we'll do plus equals and we won't do Apple this time we'll do pair we'll run that thing again so now if we call Dollar fruits we can see we've got apple and pear so we're adding stuff to this array that we can then call in future points within the script itself now we could if we wanted to actually declare a variable in this way and actually add some kind of starting data to it if we wanted so let's just restart that shell let's go and kill that terminal and let's go and whoopsie there we go completely kill it let's go restart that shell again so we're doing a DOT pwsh it's a PS version table here we'll just check out version one we're in version Powershell version 7.3.5 so the latest version it's all good you don't need the latest version to do this this will all work on I think all the way back to like Powershell too uh so let's try another array let's do dollar veg okay and we will declare not a blank array this time but we will declare and a rate that's got some existing data in it so let's do tomato I know I know fruit or vegetable who knows uh potato and let's go do one more veg let's do carrot okay so let's go run that dollar veg and we can go call Dollar veg here tomato potato and carrot we could also add additional stuff to it as well so if we again we did dollar veg and we went plus equals and we added something else to this uh maybe we want to add another vegetable uh eggplant okay or yeah obviously whichever one you want so if we add that eggplant in here and then call Dollar veg again we can see it's added to the Bottom now each of these have individual positions okay so this dollar veg is now currently loaded into memory with position zero through to position four all right so we can call individual positions inside this variable so if for example we called dollar veg now all this is kind of loaded and we called position zero you can see I'm calling tomato if I call position three on the other hand I'm calling Ed plant so tomato potato carrot and what got added afterwards eggplant inside here we can also call multiple so if I do for example zero dot dot two that's actually going to call 0 1 and 2 everything in between as well so that's kind of cool we can also replace things actually inside that as well so say I don't want a potato there anymore we know that potato is at position one so we could now do something like this we could say array one is now equal to uh not potato not carrot not eggplant I need another vegetable Sprout Okay add that in there now if I go and call Dollar veg you should see that I've got tomato Sprout carrot and eggplant so we can actually edit things at individual positions here in the variable itself now just another note here with variables you don't have to have the same data type inside each individual slot so you could do something like dollar stuff if I wanted to and do a position one I've got Apple at position two I've got 2.4 at position three I've got think and at the next position I may have a currency of 10 pounds okay and now if I can run this variable there is something that goes a bit wonky here this is the 10 pound option I'd have to declare the type but if I change this around go and run that let's do dollar stuff you'll be able to see I've got Apple 2.4 thing and 10. each one of these has their own individual type so if we run something like stuff zero dot get type I'm gonna run that we can see that this is actually a string if I go and run stuff one get type you'll see this is actually a double which is a number that is actually got uh about um start again this is a number that's actually got a decimal point in it this is something called system.double if I go and run something like uh three here you'll see this is an into 32 or a 32-bit integer I.E the number of 10. that's not binary one zero that's 10. so if I clear this off now there is another thing we can actually play with here all right so let's come up to this selection of code and let's just comment this out we'll use a big block of comments here to do this it's a little commenting trick so if you use the um open carrot and then close carrot with a pound sign for the Americans or a hash sign for everybody else you can comment at whole blocks and you can actually collapse that if you're using partial Isa you need a plug-in to collapse that this is an interesting trick if I just launch Powershell ISE here on my computer and I go and take this same block of code with powers.isc if you paste it in there notice you actually get this little option where you can actually collapse It Down vs code does not do that by default but PowerShot ISC does anyway continuing on we need to talk about 2D arrays okay so we can change the way this works what we've just seen is a one-dimensional array all right it's just a single line with multiple different elements inside it we can create 2D arrays this is kind of like matrices okay of data if you can imagine just grids of boxes so we've got individual rows and we've got columns actually within this 2D array now to define a 2d array things look a little bit different so what we need for a multi-dimensional array is an actual grid okay so think of it a little bit like this so if I do um XXX that would be for example a grid okay so you've got different positions here as well you've got a line of zero and a line of one and then you've actually got individual positions within that grid so you've got a position of zero and a position of one so each element within this grid this two by two grid is going to have its own specific key so we can create one of these by using um it's just two and dollar array for array we need to use the new object command now this is going to create a new Powershell object that's going to allow us to store information and the object type I want okay is this so doing that little um two square brackets for the comma inside it defines this as a multi-dimensional array and I've got to decide how big that grid is actually going to be before I can actually insert data into it so unlike a normal array I've got to kind of figure out how much data I've got before I go start inserting stuff into this you can add stuff later to it but you're going to need to add some code around it with some for each Loops to be able to do it so this is going to be a two by two array so this is actually going to be a 2d array of length four it's going to be length four because we're going to have four individual elements inside here total of our grid so let's start adding some data in now the way we're going to do that is we're going to start in the top left of this I'm going to go 0 0 okay so that's point zero on the row and point zero on the column okay and we're going to add in the number of 10. and then we're going to continue on we're going to do a and we're going to add 0 comma one and that's going to be something like dollar false okay we'll add a Boolean for that one then we'll do dollar a and we'll do one zero so the next road down but zero is one in and that's going to be red and we'll just move bread zero and we'll just do a dollar a and we'll do one more thing in here we'll do uh one one to define the whole grid of information and we'll call that blue okay so now if we're going to run this code here that should load everything in for me there we go clear that screen out now if I just call that variable so call a I'm going to get all four of those things come out 10 false red blue it's going to work its way through that grid but I can actually call individual objects inside that so zero one and I'll be able to see that that is actually equal to false or if I go to one one that's actually going to be equal to Blue all right so we now have a slightly more complex example here of 2D arrays and a more practical example of 2D arrays so let's kind of run this see what the output is and then we'll step through and kind of explain everything that this is actually doing so if I run this whole thing now you'll notice the output that I get here is student one grade student two grades and student three grade so let's just scroll back up here Michelle's been getting annoying here we go so student one grades student two grades and student three grades down here let's just rerun that to make sure it looks a little bit nicer uh oops there we go shells playing up at me right so how does this kind of work well we've got the top here is if we've got our 2D array all right so notice this first block here this at symbol up here along with these two uh parentheses this is defining the whole array and then we've got different positions here so we've got position zero we've got position one and we've got position two these are gonna be the rows in our 2D array and then inside that what we've actually got is we've got the data so we've got things like 85 92 76 88 each of these are going to have positions as well so that would be position zero position one position two and position three so we've got rows and we've got columns going on down here so we want to process through each of these individual things and output the data in a nice format to be able to be a read that in the shell so the first thing we've got here is we've got our four leap for Loop and we've got four dollar row zero row less than grays.length row plus plus so what we want to do is we want to process through each individual row down here so here's our row we're starting at row zero so we're going to process through this specific row here if the row is less than grades.length and gray stop length down here we know is actually going to be at zero one two three because computers start at zero but it's four elements inside there it's going to write the host student grades so let's get rid of some of this so far and let's go and control to that make this a little bit neater up so write students row plus one and then grade so that's going to write this element down here so remember this would be zero at this point because it's dollar row and the first row here is zero so we've got to add plus one on there for it to kind of make sense because we don't count it star to get zero but computers do we just add one onto there to make it make a little bit more sense now we're going to start processing that first element there this first collection of student stuff and we want to process through each of these individual numbers and print them out accordingly so what we have is we have a second for Loop we have a for Loop inside the for Loop because we want to Loop through each individual row and then we want to Loop through each individual number in each individual row okay so the second for Loop is to define the columns which are these entry points here and for each column starting at zero and working through one two three four row dot length column plus plus so step through each individual column here then depending on the position of the column so the first position is going to be zero so we're going to change this to math the second position going to be one we're going to change this to English and so on and so forth this is using a switch case to do this rather than using an if else Loop otherwise we would have lots of cell if else Loops down here the grade is finally going to be defined as grades dollar row dollar column so at the first point here this first grade is going to be row 0 column zero so it's going to access this specific grade up here and then what it's going to do is it's going to actually set that grade down here and we're going to also write dollar subject and we can see that dollar subject is actually going to be set here to math so this is going to write out math with a grade of 85 so that's a lot of green on the on the table there let's get rid of some of that and if we just change that to Red you can see that that's actually going to write out math at 85. it's then going to continue stepping through this thanks to these magic little plus pluses what that's going to do it's going to iterate the row from 0 to 1 to 2 to 3 every time there's a loop going on through there so we're done with zero we're done with zero there it's now going to iterate over to one so for each column we're going to column plus plus and once this is looped through one two three four times I.E the length of the row then this four loop at the top can kick in and kick down to the next position in the 2D array so that's a kind of example there for how 2D arrays can actually take data store data and how you can read out of them by using these nested for Loops to actually pull data out but let's go and have a look at another way of actually storing some data inside Powershell all right so another way of doing data storage here is something called a hash table now sometimes hash tables also get called key value Pairs and that would be the same sort of thing we have a um a hash table up here at the top we have John Alice and Bob inside student grades now this looks similar to what we saw before in an array but there's a very subtle difference here so if we look back here in an array arrays used parentheticals okay there's normal standard brackety kind of things on the other hand hash tables here use curly brackets now they've got some strange names as well if you look these up on Wikipedia some people call these Scottish brackets I know it sounds weird but honestly go look at the Wikipedia article I promise you it says Scottish brackets curly brackets mustache braces some people even call these things um seagull brackets I don't know they'll always be curly brackets but what we've got inside here is we've got a key John and a value 85 a key Alice and a value of 92 and a key Bob of a value 76. so instead of accessing individual items in here using things like zero one and two we're going to access elements in here by using these Keys instead and that's that's the difference here with hash tables versus normal arrays so if I go and load this in here and just load that line in I'll just go do dollar student grades okay I'm gonna run that you'll see that I've got name and value returned here as I said key value name value it depends on what language you're using what book you're reading but the same thing what I can do is I can actually now actually look at individual things inside here so we can access values by using specific keys so in this case John's grade is equal to student grades John so if we just go run that block there you'll see what's returned is 85. it doesn't return John equals 85 what it's done is it's accessed that specific key and returned that specific value to my shell here so now if we Define dollar John grade into equal students grades John run that then what we'll actually be able to see is John grade will return just 85 and if we want a John grade dot get type on this just to see what returns whoops it helps if I can type this is actually returning an INT 32. so if I go and run for example student grades John here paste that here and then run another get type you'll see again in 32 returned but if we go and run something like dollar student grades and do a DOT get type on the whole thing you'll see this is returning a hash table so what we can also do is we can modify things so we can see down here Bob is currently set to 76. and if we go and return dollar student grades you'll see that yeah Bob is set to 76 but we can modify this so we can modify a specific value of Bob to 88 if I can run that one line there and then go and rerun student grades what you'll see is we've got Bob value of 88 so hash tables the data inside them is easy to manipulate we can also add new lines so if you want to add Lisa here Lisa is now equal to 90. go and run that and we go Dollar Rent student grades and go and execute this you'll see we've now got Lisa being added in with a value of 90. now this is no different but due to what we did with arrays but there is one key point and it's the next element dude next element down here we can remove a key value pair okay we can go in here and we can actually remove an individual item inside this hash table we can't do that with arrays to do that we'd have to rebuild the array itself there is actually something else that we can actually do with arrays there's a special array type that allows us to remove stuff but that's something slightly different and we'll check that out in a minute but if we want to do this with key value pairs we can just use this dot remove element down here and we can actually remove Alice directly from student grades run that again and Alice has now gone we can also iterate over the entire hash table by running this little four each Loop for each key in student grades.keys so that's each individual key down here write the value host of key and value so if we go and run that so Bob 88 leads to 90 and John 85. that's going to Output the whole thing for me okay so those are hash tables you'll see hash tables returned in many different things if you start interacting with apis and rest apis a lot of data gets returned within hash tables foreign so we're going to step back to a raise for a moment because I said there was something I couldn't do and that was to actually remove the elements from a basic array now there is sort of different ways of doing this with Powershell version seven but if you're using Powershell five one for your code internally you might bump into these problems and this is still applicable through to Powershell seven as well so it'll work all the way up so if we look at this array of dollar stuff right we're going to run that again we've got a lot of stuff inside here we've got Apple better repair that's fine okay and again if we do get type inside here for this specific method don't run that it's an object of two square brackets and a system dot array now what about if I want to remove something so something very basic might be for example remove and if we try and remove Apple here from this array we're going to get narrow the exception coin is collection was of a fixed size now you can tell if an array is in a fixed size because what you can do do that what you can do is dollar stuff and run this is fixed size okay run that it tells you true or false now what this fixed size means it doesn't mean that we can't add stuff to it but it does mean that we can't remove stuff from it all right but we can use a special array type to get around that now if we didn't use a normal array like this we didn't just add stuff to Dollar stuff we did something like this specifically code in here dollar list is equal to new object system.collections.araylist this is where the magic comes now if I go and run this just a single line here okay and now I do dollar list dot get type all right you'll notice this is an arraylist now what about happens if I run dollar list dot is fixed size false so we can remove things but let's add stuff to this first now there's some more functionality we can use dollar list dot add okay and then what I'm going to do is I want to add Apple in here and then I want to add some more elements down here so I'm just going to copy that I'm going to come down here paste that in there come down here paste that in there and we're going to bring that one over here cut that up there pop that in there and yes I know there is way faster ways of doing this put some magic brackets inside here oh if you don't know what I did there if you're in vs code if you hold Ctrl and ALT and press up or down we can get multiple cut and multiple things at once okay that's quite a neat little trick to do if you need to edit multiple lines in one go so if we go and add all of those things in run all of that code it runs through it all we can now do dollar list we should see apple berry pair now it's just a normal array so we can still iterate over it we can still do position zero we can still do position one that's fine but what we can also do as we can actually look at accessing items in a different way and removing items in a different way so maybe I want to remove berry so what I can do now is I can do dollar list remove all right and I can remove this based on position so I could say something like remove one all right and I'm going to go run that remove one remove at position one so delete whatever in position one now if I do dollar list again you'll see that I'm retrieving only apple and pear because Berry has been removed from that arraylist right you could also remove things by the actual name of the thing that's in there so you can do not remove ads you can actually do the command just shred remove so if you do remove Apple run that again and put this put some curlies on some colors some double quotes to denote a string run that thing do dollar list and we have just pair remaining for us so this concludes looking at some different ways of storing data we went through and had a look at basic uh one-dimensional arrays in here we also went through and had a look at how to create 2D arrays like matrixes of data inside here we then went and had a bit more of an example of these 2D arrays and how to iterate over multiple elements within those 2D arrays via their rows and Via their columns we also had a look in here at a hash table remember the main difference there is we've got curly brackets rather than parentheses or normal brackets on there and then finally we took a look at this final thing called an array list which allows us to manipulate rays in a little bit more detail actually remove items from them there is reasons why you would use an arraylist rather than a normal array and vice versa the main thing is really performance a radius aren't as fast as normal arrays when you're actually dealing with lots of data but you have to be dealing with lots of data for that to actually start to be noticeable anyway I hope you enjoyed this content and you'll join me next time for some more tutorials on Powershell or on Microsoft azure and you know the routine hashtag like And subscribe and I hope you enjoyed this video and will join me next time goodbye thank you
Info
Channel: Mike in the Cloud
Views: 1,223
Rating: undefined out of 5
Keywords: PowerShell arrays, PowerShell scripting, Data storage, Data manipulation, 1-dimensional arrays, 2-dimensional arrays, Hash tables, System.Collections.ArrayList, PowerShell data structures, PowerShell tips and tricks, PowerShell tutorials, PowerShell scripting techniques, PowerShell programming, PowerShell automation, PowerShell scripting best practices
Id: vcvEuoA2zp8
Channel Id: undefined
Length: 28min 34sec (1714 seconds)
Published: Thu Jul 13 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.