Q&A

Q1:Most of the functions you described on this lesson seem to work only with lists. Can I use them with hashes as well?
A1: Hashes and lists are interchangeable in the sense that a hash will be unwound into its component parts when used as a list, and then the elements will be paired up into keys and values when the list becomes a hash again. So technically, yes, you can use many of these functions with hashes. The result, however, might not be what you expect and might not be very useful. For example, the pop function on a hash will unwind the hash into a list, and then pop off the last item in that list (the last key in the hash)—but since you don't know what order the hash's keys will appear in, the result is unpredictable at best. Better to use hash slices or the delete function to modify hashes, or use these functions on lists of the keys or values.
Q2:I'm trying to use reverse on a list of strings. They're not reversing; it's like I never called the function.
A2: Are you sure you're using the reverse function on a string, and not on a list of one element? A list of one element is still a list, and the reverse function will happily reverse that list for you—giving you the same string you put into it. Make sure you're calling reverse in a scalar context, and if you're not, change the context with a different bit of code or with the scalar function.
Q3:In a previous lesson, you described the $" variable, which is used to set the separator character for list elements. It shows up when you interpolate a list inside a string. How is the join function different from this technique?
A3: If you're looking at the end result, it's not. Both will provide a way of “flattening” a list into a string with some character in between each of the list elements. The join function, however, is more efficient for this purpose because the string in question doesn't have to be interpolated for variables before it's expanded.
..................Content has been hidden....................

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