Difference between revisions of "Talk:Arrays (Papyrus)"
imported>Cdcooley |
imported>Fg109 |
||
Line 88: | Line 88: | ||
</source> | </source> | ||
===Examples of working with every item in the array | ===Examples of working with every item in the array=== | ||
The real power of lists only appears when you need to do the same thing with every item in the list. We can loop through all of the items in the list by their index position. | The real power of lists only appears when you need to do the same thing with every item in the list. We can loop through all of the items in the list by their index position. |
Revision as of 09:14, 26 May 2012
I appreciate that this page has been created because I never really understood this Arrays thing... but I still barely get it. I think that this page needs a rewrite and it needs to be dumbed down.
First, when you write something "for begginers" don't give them a long lecture about how computer memory works. I know that you wanted people to understand the whole concept but it's really hard for newbies. And if you do something like this please, simply try to avoid sentences like "...since you'd need to construct identifiers at runtime by concatenating the descripting name with the changing index". Sometimes I just didn't know what it was about. You're talking about arrays and then suddenly: "Arrays are, memory-wise, just a bunch of bytes stacked together, continuous in memory (virtual memory, that is, physical memory can get messy). From now on, I'll just refer to it as memory."
Second, when you get to the technical part of the page, scripts and all, could you make it more orderly? Like, start with how you define an array, then tell how to define its value, then how do some other stuff... I mean, get from the simplest stuff to the most complicated. Right now I think there's everything in this article but I still don't understand it. Try to incorporate new things gradually. I know that this isn't a tutorial but still, many people are going to learn from this. Domius 14:16, 17 February 2012 (EST)
For beginners who didn't major in CS
No offense meant to the person who wrote the For Beginners section, but I agree it's still far too technical for beginners. Here's my attempt.
Arrays are how programmers store lists of related things. Compare a normal person's grocery list:
Bread
Milk
Eggs
Bananas
Some people are a little more organized:
Grocery List
------------
1. Bread
2. Milk
3. Eggs
4. Bananas
Programmers have to be even more obsessive because computes are dumb and need every little detail written down.
Defining arrays
First the computer needs to know what kind of things will be in the list, that it will be a list, the name of the list.
FoodItem[] GroceryList
Declaring arrays
But many times we need to also tell the computer to make enough storage space to hold the number of items we plan to store.
FoodItem[] GroceryList = new Fooditem[4]
Setting individual values
Then the items are stored in the numbered locations, but it's easier for the computer to start counting at zero instead of one, and has to be reminded of which list every single time.
GroceryList[0] = "Bread"
GroceryList[1] = "Milk"
GroceryList[2] = "Eggs"
GroceryList[3] = "Bananas"
Checking the length
Finding out how many items are in the list is easy. (Actually it's how many slots are available, even if some are blank.)
GroceryList.length
Getting individual values
Getting the values back isn't bad as long as you remember that the computer starts counting at zero.
FoodItem TheThirdItem = GroceryList[2]
Creating functions that expect arrays
When you need to tell the computer to expect a list, you put the square brackets after the name of the type of items the list will hold.
Function BuyThese(FoodItem[] ShoppingListForToday)
Sending whole arrays to a function
But to do something with an entire list you would just use it's name.
BuyThese(GroceryList)
Examples of working with every item in the array
The real power of lists only appears when you need to do the same thing with every item in the list. We can loop through all of the items in the list by their index position.
Function BuyThese(FoodItem[] ShoppingListForToday)
int itemNumber = 0
while itemNumber < ShoppingListForToday.Length
FindTheItemAndPutItInYourCart(ShoppingListForToday[itemNumber])
itemNumber += 1
endwhile
GoThroughCheckoutAndPay()
EndFunction
Making it a little more like the game's context for tracking items will make it more complex. So programmers usually shorten those names and might even give the list a name that makes more sense based on how it's used just to compensate.
Function BuyThese(FoodItem[] item) ; item is a list of fooditems
int totalCost = 0 ; we'll want to know this at the end
int n = 0 ; start at the beginning
while n < list.Length ; and go to the end of the list
if TheStore.GetItemCount(item[n]) > 0 ; if item is in stock
TheStore.RemoveItem(item[n], 1) ; take one from the shelf
TheCart.AddItem(item[n], 1) ; put it in our cart
totalCost += item[n].GetGoldValue() ; record the price
item[n] = None ; mark it off of our list
endif
n += 1 ; go on to the next item
endwhile
GoThroughCheckoutAndPay() ; hopefully we'll have enough money
EndFunction
Other considerations
But passing that list into the BuyThese function really is like handing someone a list. If code inside that function changes or removes some of the items, then they are changed everywhere that list is referenced. We aren't making copies of the list, just letting different parts of the code see it (and some may call it different things, but it's the same list). So after we've called "BuyThese(GroceryList)" we'll be able to see if anything wasn't available.
int i = 0
while i < GroceryList.Length
if GroceryList[i] ; it wasn't changed to None
Debug.Notification("We weren't able to buy the " + GroceryList[i].GetName())
endif
i += 1
endwhile
Some special limitations are that each list can only have up to 128 items, you must use a literal number with the "new" command, and that you can't have an array of arrays.
--Cdcooley 23:24, 25 May 2012 (EDT)