Q&A

Q1: What's the difference between the undefined value and undef?
A1: Not a whole lot. The undefined value is what gets put in variables, or in array or hash value locations when there isn't an actual value—if you use one without initializing it, or if you add elements to an array past the boundaries of that array. If you want to explicitly use the undefined value, for example, to undefine a variable or to include that value in an array, you use the undef function. Because of the close relationship between the undefined value and undef, it's very common to see undef used to mean the undefined value (as in “the last three elements of that array are undef”). In real code, if you use undef anywhere you want an undefined value, you won't go wrong.
Q2:I want to create an array of arrays.
A2: You can't do that. Well, not right now. To create arrays of arrays, or arrays of hashes, or any kind of nested data structures, you need to use references. You won't learn about references for a while yet, so just sit tight for now. We'll cover references in Chapter 19.
Q3:Why, when you delete an element from an array using delete, does it just undef the element? Why doesn't it delete it and reconstruct the array? Delete means delete, doesn't it?
A3: Not necessarily. Array elements are indexed by number, and the relationship between an element and its index might be important. By deleting an element, you'd end up renumbering all the elements in the farther down in the array, which might not be what you want to do. The delete function does the safe thing and just adds an undef placeholder (except if the element to be deleted is the last element, in which case it's considered safe to remove it). You can always reconstruct the array if you really want those elements deleted.
Q4:Augh! I don't understand list and scalar context. If different operations can do different things, and there are no rules for how lists and scalars behave in each other's contexts, doesn't that mean I have to remember what every operation does for every context?
A4: Uh, well, yes. No. Kind of. If list versus scalar context is totally abhorrent to you, in any given script, you can usually avoid most of the more esoteric instances of context. Remember how to use the few that are important (getting the length of an array, for example), and look up the rest when something doesn't work right.

If you end up having to read other people's Perl code, however, chances are good you'll end up needing to keep context in mind and watch out for sneaky contexts.

Q5:I want to find the number of elements in a list, so I did “length @array…”
A5: Hold it right there! The length function is a fine function—for strings and numbers. To find the number of elements in a list, you should be using the array variable in a scalar context: $numelements = @array.
Q6:How do I search an array for a specific element?
A6: One way would be to iterate over the array using a foreach or while loop, and test each element of that array in turn. Perl also has a function called grep (after the Unix search command) which will do this for you. You'll learn more about grep on Day 11, “Creating and Using Subroutines.”
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset