Phantasmal MUD Lib for DGD
|
Phantasmal Site > DGD > DGD Reference Manual (legacy) > Arrays 3.7 ArraysAn array is a list of values, either all of the same type or of multiple types. An array makes it easy to reference values by their number — for instance, it's easy to say "give me the fourth value in the array". However, an array is slow when you add values or remove them. Let's write a function to count digits in a string. Go ahead and change myobj.c (or make a new file) to the following: mixed count_digits(string str) { int *num_count; int ctr; object user; num_count = allocate_int(10); for(ctr = 0; ctr < strlen(str); ctr++) { if((str[ctr] <= '9') && (str[ctr] >= '0')) { num_count[str[ctr] - '0']++; } } user = this_user(); user->message("Count: ["); for(ctr = 0; ctr < 10; ctr++) { user->message(num_count[ctr] + " "); } user->message("]\n"); } To call this file, first compile it. So you'll type compile /usr/admin/myobj.c. If you're not using myobj.c as the filename, substitute the one you're using. You should get a result like $34 = </usr/admin/myobj>. Now, using the history variable you get back (we'll pretend it's $34 here), type $34->count_digits("0123456789"), or the equivalent with your own history variable number. You should see a result like the following: Count: [1 1 1 1 1 1 1 1 1 1 ] $36 = nil Since the function doesn't explicitly return anything, the
history variable result is nil. But since the function
calls If you look at the line in the code starting with There are two new types of variable declaration in the code to count_digits. There is a variable type called object, like mixed or int. A variable of type object will hold a compiled DGD object, such as a connection object, or the object that myobj.c compiles into. In count_digits, it holds a user object like the one you passed to the function in the last section. You'll notice that the variable declaration for num_count starts with int, but that there's an asterisk in front of the variable name. The asterisk means that num_count isn't an integer, but rather an array of integers. Defining num_count doesn't give it a value. Arrays, like strings, are assigned nil by default, and have to be initialized to something. One way to assign reasonable values to an array of integers is the allocate_int function, which returns an array of zeroes. The array is as long as the number in parentheses is big. For the example above, that means it returns an array of ten zeroes. There are a couple of other new things in the code above. We'll
tackle them one at a time. The first is Another is the zero in single quotes. Remember that LPC characters are represented as ASCII-encoded integers. A character in single quotes will evaluate the that character's integer. Try typing code 'A' and you should get a result of 65, just like in the section on handling strings. The 'if' statement checks to make sure that the character is a number between 0 and 9. That way letter, spaces and punctuation will be ignored. The final new feature of the code is the square brackets, called
the array dereference operator. The idea is that for an
array, like a string, you can take a single element from it. So if
an array called arr contains a zero followed by four
nines, then the value of In The second dereference, the one outside, is an array
dereference, not a string dereference, but they work the same way
if you think of a string as being like an array of ASCII-encoded
characters. Since subtracting the character To write an array value in LPC, you surround the entries with
parentheses and curly braces and separate the entries with commas.
So the array mentioned above would be written Arrays can also be added to each other like strings. Adding them will make a new array which is all the elements of the first array followed by all the elements of the second array. So code ({ 1, 2, 3 }) + ({ 7, 8, 9 }) will print a result like $40 = ({ 1, 2, 3, 7, 8, 9 }).
|