3.1.12 Printing Elements of a List or an Array
Of course, we can print elements of a list or an array by looping through the elements of the array using a for loop. The following program loops through every element of the list @friends and prints the elements one by one, one per line.
Program 3.4
#!/usr/bin/perl
use strict;
my @friends;
@friends = ('John Hart', 'Jeff Ellis', 'Brett Walters',
'Blake Escort', 'Tom Toybee');
for ($i=0; $i <= $#friends; $i++){
print "$friends[$i]\n";
}
In this program, the expression $friends[$i] inside the double quotes is interpolated to obtain the element with index $i in the array @friends.
A simpler way to print every element of an array is to simply use the print function on the whole array.
Program 3.5
#!/usr/bin/perl
use strict;
my @friends;
@friends = ('John Hart', 'Jeff Ellis', 'Brett Walters', 'Blake Escort',
'Tom Toybee');
print @friends;
print "\n";
This program prints all elements of the list @friends in one single line without any space between two consecutive elements. However, if we write the array variable inside double quotes, i.e., write
print "@friends"; print "\n";
the elements of the array are written out in a single line, but this time, two consecutive elements of the array are separated from each other by an empty space, the default list element separator. That is, an array variable is interpolated inside a string using the default element separator.
We can change the value of the list item separator by assigning a value to the special variable $". We can set $" to a sequence of one or more characters. The list items will be printed separated by the sequence of characters specified. The list separator is not printed after the last element of the list. It can be assigned several times inside a program. We should note that setting the list element separator does not have any effect unless a list is printed inside double quotes. Also, the list separator in effect at a certain time in the program will affect every list printed.
Program 3.6
#!/usr/bin/perl
#file friends8.pl
use strict;
my @friends;
@friends = ('John Hart', 'Jeff Ellis', 'Brett Walters',
'Blake Escort', 'Tom Toybee');
#print the list elements separated by a comma and a space
$" = ", ";
print "@friends";
print "\n";
#print the list elements in the form of an HTML list
$" = "\n- \n
- "; print "@friends"; print " \n
First we set $" to print the list elements separated by a comma and a space character. Next, we set the value of $" to </li>\n<li>. This will cause the list items to be printed separated by a newline character and the sequence <li>, the HTML list item tag. Since the list element separator is not printed before the first element, we print <li> before the first element. Also, we print the tag <ul> to start an HTML unordered list, and the tag </ul> at the end to end the unordered list. A program fragment like this can be used to produce an HTML list inside a Perl program. The output of the program is given below.
John Hart, Jeff Ellis, Brett Walters, Blake Escort, Tom Toybee
- John Hart
- Jeff Ellis
- Brett Walters
- Blake Escort
- Tom Toybee
Perl also has a special variable $, that can be used to set the value of the separator string printed between two array elements for an array variable that is not interpolated. That is, the separator string is used when an array variable is printed directly without any double quotes. The following program has the exact same HTML output as the immediately previous one.
#!/usr/bin/perl
#file friends11.pl
use strict;
my @friends;
@friends = ('John Hart', 'Jeff Ellis', 'Brett Walters',
'Blake Escort', 'Tom Toybee');
#print the list elements separated by a comma and a space
$" = ", ";
print "@friends";
print "\n";
#print the list elements in the form of an HTML list
$, = "\n- \n
- "; print @friends; print " \n
