Getting Elements Out of an Array

 <  Day Day Up  >  

So far in this hour , you've been slinging around whole arrays and lists and putting information into arrays. How can you get that information back out?

One way to get the contents of the entire array is to put the array variable in double quotation marks:

 print "@array"; 

An array in double quotes is interpolated, and its elements are returned separated by spaces. This example prints the elements of @array with a space separating each element.

Many times, though, you need to get to individual elements of arrays. You may need to search for an element, change the value of an element, or to add or remove individual elements in an array.

Individual elements in an array are accessed by a numeric index. The index for array elements starts at the number 0 and increases by 1 for each additional element. Each element of the array has an index value, as shown here:

The number of elements in an array is limited only by your system's memory. To access an element, you use the syntax

 $  array  [  index  ] 

where array is the array name and index is the index of the element you want (also called a subscript ). The array doesn't have to exist before you refer to individual elements; if it does not already exist, it just automagically springs into existence. Some examples of accessing array elements follow:

 @trees=qw(oak cedar maple apple); print $trees[0];        # Prints "oak" print $trees[3];        # Prints "apple". $trees[4]='pine'; 

Notice that to talk about an individual element of @trees , the code uses a $ . "I thought the $ marker was usually reserved for scalars; what's going on?" you might ask. The answer is that the $ in $trees[3] does refer to a scalar: one scalar value within @TRees . (Scalars are also indicated by a dollar sign because they're singular as well. You should notice a pattern here.)

At the beginning of this hour, you discovered that scalars and arrays can have the same variable names and remain unrelated. Perl can tell the difference between $trees , a scalar variable that has nothing to do with the @trees array, and $trees[0] , the first element in the @trees array, because of the square brackets in $TRees[0] . Perl knows that you're taking about the first element of @trees and not talking about $TRees at all.

You can also talk about a subgroup within an array, called a slice . To take a slice of an array, you use both the @ type identifier ”to indicate that you're talking about a group of things ”and square brackets ”to indicate you're talking about individual elements of an array, as shown here:

 @trees=qw(oak cedar maple apple cherry pine peach fir); @trees[3,4,6];          # Just the fruit trees @conifers=@trees[5,7];  # Just the conifers 

Finding the End of an Array

Sometimes you need to find the end of the array ”for example, to see how many trees are in the @trees array or to cut some trees out of the @trees array. Perl provides a couple of mechanisms for finding the end. The first is a special variable in the form $# arrayname . It returns the number of the last valid index of the array. Check out this example:

 @trees=qw(oak cedar maple apple cherry pine peach fir); print $#trees; 

This example contains eight elements, but you must remember that arrays are numbered starting at 0. So the preceding example prints the number 7 . Modifying the value of $#trees changes the length of the array. Making it smaller truncates the array at whatever index you specify, and making it larger gives the array more elements. The newly added elements all have their values set to undef .

The other method of finding the size of an array is to use the array variable in a place where a scalar is expected:

 $size=@array; 

This puts the number of elements in @array into $size . This takes advantage of a Perl concept called context, explained in the next section.

By the Way

You can also specify negative indexes for arrays. Negative index numbers start counting from the end of the array and work backward. For example, $array[-1] is the last element of @array , $array[-2] is the next to the last element, and so on.


Learning More about Context

What is context ? Context means the things that surround an item of interest to help define what that item means. For example, seeing a man in surgical scrubs can have different meanings depending on where he is: In a hospital, the fact that the man is wearing scrubs might mean that he's a doctor; at a Halloween party, he could be just another party guest in costume.

Human language uses context to help determine the meaning of words. For example, the word level can have several different meanings depending on how it's used and what context it's in:

  • The carpenter used a level to hang the door straight.

  • The moderator spoke in a level tone.

  • The water in the pool was at waist level.

It's the same word each time, but the meaning has changed. It becomes a noun, an adjective, and a different kind of noun depending on how it's used in a sentence .

Perl is also sensitive to context. Functions and operators in Perl can behave differently depending on what context they're used in. The two most important contexts in Perl are list context and scalar context .

As you've seen, you can use one operator ”the equals sign ”to perform assignment with both arrays and scalars. The type of expression (list or scalar) on the left side of the assignment operator determines what context the things on the right side are evaluated in, as shown in the following lines of code:

 $a=$b;       # Scalar on the left: this is scalar context. @foo=@bar;   # Array on the left: this is list context ($a)=@foo;   # List on the left: this is also list context. $b=@bar;     # Scalar on the left: this is scalar context. 

The last line is interesting, because it puts an array into scalar context. As was stated in the previous section, evaluating an array in a scalar context returns the number of elements in the array.

More about the Size and End of an Array

Observe $a and $b in the following few lines of code; they do almost the same thing:

 @foo=qw( water cola juice lemonade ); $a=@foo; $b=$#foo; print "$a\n"; print "$b\n"; 

At the end of this code, $a contains the number 4, and $b contains the number 3. Why the difference? $a is @foo evaluated in a scalar context, and it contains the number of elements. $b , on the other hand, is set to the index of the last element, and indexes start counting at 0.

Because arrays in a scalar context return the number of elements in the array, testing whether an array contains elements becomes this simple:

 @mydata=qw( oats peas beans barley ); if (@mydata) {     print "The array has elements!\n"; } 

Here, the array @mydata is evaluated as a scalar, and it returns the number of elements ”in this case, 4. The number 4 evaluates to true in an if statement, and the body of the if block is run.

By the Way

Actually, @mydata here is used in a special kind of scalar context called a Boolean context, but it behaves the same way. Boolean context occurs when Perl expects a true or false value, such as in an if statement's test expression. One other context, called void context, will be explained in Hour 9, "More Functions and Operators."


Context with Operators and Functions

Many of Perl's operators and functions force their arguments to be either scalar context or list context. Sometimes the operators or functions behave differently depending on what context they're in. Some functions you've already encountered have these properties; however, this fact hasn't been important until now, because they have only had scalars to work on.

The print function expects a list as an argument. It doesn't particularly matter what context the list is evaluated in, though. So printing an array with print like this causes the array to be evaluated in a list context, yielding the elements of @foo :

 print @foo; 

You can use a special pseudofunction called scalar to force something into a scalar context:

 print scalar(@foo); 

This example prints the number of elements in @foo . The scalar function forces @foo to be evaluated in a scalar context, so @foo returns the number of elements in @foo . Then the print function simply prints the number returned.

The chomp function you learned about in Hour 2, "Perl's Building Blocks: Numbers and Strings," takes either an array or a scalar as an argument. If chomp is presented with a scalar, it removes the record separator from the end of the scalar. If it is presented with an array, it removes the record separator from the end of each scalar in the array.

 <  Day Day Up  >  


SAMS Teach Yourself Perl in 24 Hours
Sams Teach Yourself Perl in 24 Hours (3rd Edition)
ISBN: 0672327937
EAN: 2147483647
Year: 2005
Pages: 241

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net