4. Arrays And Lists


1. The sort() function expects a list to be provided as input,
and it returns a sorted version of that list.
Run the following program slist.pl:
#! /usr/bin/perl
$USAGE = "Usage: slist.pl";
$project1 = "Spacestation";
$project2 = "Hubble";
$project3 = "Mars";
print ($project1, $project2, $project3); print "\n";
print sort ($project1, $project2, $project3); print "\n";
# END OF slist.pl

(a). How are the elements of the list formatted as far as spacing is
concerned?

(b). Does the last print line appear to sort the elements properly?

2. Compare the operation of the following program slist2.pl with
the program slist.pl.
What formatting differences do you notice?
#! /usr/bin/perl
$USAGE = "Usage: slist2.pl";
$project1 = "Spacestation";
$project2 = "Hubble";
$project3 = "Mars";
@rawlist = ($project1, $project2, $project3);
print "Raw list: @rawlist\n";
@cookedlist = sort @rawlist;
print "Sorted list: @cookedlist\n";
# END OF slist2.pl

3. the qw{  } operator quotes each of the items in its list. 
Run the qlist.pl program.
#! /usr/bin/perl
$USAGE = "Usage: qlist.pl";
# print1: print a list.
print ("Houston", "Miami", "Huntsville", "Moscow"); print "\n";
# print2: This should do the same thing.
print qw{Houston Miami Huntsville Moscow}; print "\n";
# print3: Some quote redundancy
print qw{"Houston", "Miami", "Huntsville", "Moscow"}; print "\n";
# print4: Some of each kind of quoting
print qw{Ames, Johnson}, ("Langley", "Andrews", "Jet Propulsion Labs", "\n");
# END OF qlist.pl

(a). Do the 1st and 2nd print statements produce the same output?

(b). What happened with the 3rd print statement?

(c). What happened with the last print statement?

(d). What would happen if you executed:
$ perl -e 'print qw{Ames, Dryden, "Jet Propulsion Labs"}, "\n";'

4. Run the following program: arrayprint.pl
#! /usr/bin/perl
$USAGE = "Usage: arrayprint.pl";
@t1 = (2, 4, 8, 16, 32, 64, 128, 256);
print "table 1: [", @t1, "]\n";
print "table 1 again: [", "@t1", "]\n";
print "table 1 in a number context: [", @t1+0, "]\n";
print "If I forget that this is an array: [$t1]\n";
# END OF arrayprint.pl

(a). Is it possible to differentiate the elements of the array when
the first print statement runs?

(b) How does the 2nd print statement change things to make the content
more readable?

(c).Rewrite the 2nd print statement to eliminate the unnecessary commas
and quote marks

(d) When the array name is used as an operand in a numeric expression,
in the 3rd print statement, what value does it appear to represent?

(e) write a simple 2 line program that can test your conclusion in (d).

5. To use an array to store related data, run the following program
sponsor.pl and observe the results.
#! /usr/bin/perl
$USAGE = "Usage: sponsor.pl";
$name = "Microsoft, Inc.";
$phone = "1-800-556-7638";
$amt = int (rand 9999999) /100;
@c1  = ($name, $phone, $amt);
print "Sponsor 1 info: @c1\n";
$name = "America On-Line, Inc.";
$phone = "1-800-212-0589";
$amt = int (rand 9999999) /100;
@c2  = ($name, $phone, $amt);
print "Sponsor 2 info: @c2\n";
$name = "Amazon.com"; phone = "1-800-324-8593";
$amt = int (rand 9999999) /100;
@c3  = ($name, $phone, $amt);
print "Sponsor 3 info: @c3\n";
# END OF sponsor.pl

(a). How can the information for Sponsor 1 have been stored in @c1
without the use of the intermediate variables $name, $phone, and $amt ?

(b). What code would you use to print out just the phone number for
sponsor 2 ?

(c). Rewrite the definition of @c3 to be:
@c3 = qw{$name, $phone, $amt};
Does the outcome change at all?

6. Run the program acontext.pl given below.
#! /usr/bin/perl
$USAGE = "Usage: acontext.pl";
@t = qw(one two three);
print "Array t is: [@t]\n";

# put @t in a scalar context
$n1 = @t;
print "n1 (\@t assigned to a scalar): [$n1]\n";

# use the scalar() function
$n2 = scalar(@t);
print "n2 (\@t passed to scalar() function): [$n2]\n";

# use the special array variable $#array
$n3 = $#t;
print qq(n3 (\@t evaluated with \$\#): [$n3]\n";
#END OF acontext.pl

(a) Compare the three ways of scalarizing the array t. What does each
represent?

7. Run the program compare.pl
#! /usr/bin/perl
$USAGE = "Usage: compare.pl";
@wlist = qw(Gore, Bush, Bradley, McCain );
$, = " ";		Put a space between list elements

print "word 1: $wlist[0]\n"
print "word 2: $wlist[1]\n"
print qq(Compare "word 1 cmp word 2": ), $wlist[0] cmp $wlist[1], "\n";

print "word 3: $wlist[2]\n"
print "word 4: $wlist[3]\n"
print qq(Compare "word 3 cmp word 4": ), $wlist[2] cmp $wlist[3], "\n";

print qq(Compare "word 3 cmp word 3": ), $wlist[2] cmp $wlist[2], "\n";

#END OF compare.pl

(a) What benefit is there to using qq() quoting operator in the print lines?

(b) The cmp operator can return one of three results.  Which of the three
results indicates that the items being compared should be reversed in order?

8. Consider the following program compare2.pl
#! /usr/bin/perl
$USAGE = "Usage: compare2.pl";
$, = " ";		# Put a space between list elements

$word1 = 26; print '$wsord1 is" ', $word1, "\n";
$word2 = 104; print '$wsord2 is" ', $word2, "\n";
print qq{Compare "word1 cmp word2": }, $word1 cmp $word2, "\n";
print "When sorted: ", sort ($word1, $word2), "\n";

@nlist = qw{2 10 21 32 107 109 218 1141};
$word1 = $nlist[0]; print '$word1 is: ', $word1, "\n";
$word2 = $nlist[1]; print '$word2 is: ', $word2, "\n";
print qq(Compare "word 1 cmp word 2": ), $nlist[0] cmp $nlist[1], "\n";

print "Entire list in raw form:\n", @numlist, "\n";
print "Entire list in sorted form:\n", (sort @numlist), "\n";

#END OF compare2.pl

(a) Compare the results between the first two print statements.  Should the
list of numbers be expected to be reversed when sorted?

(b) How could the print statements containing the qq(Compare...) be rewritten
to allow them to say, literally: $word1 cmp $word2  ?

(c) Modify the above program to use <=> instead of cmp in the 3rd print
statement.  What is the result?

9. Consider the following program compare3.pl
#! /usr/bin/perl
$USAGE = "Usage: compare3.pl";
$, = " ";		# Put a space between list elements

$word1 = 26; print '$wsord1 is" ', $word1, "\n";
$word2 = 104; print '$wsord2 is" ', $word2, "\n";
print q{Compare "word1 <=> word2": }, $word1 <=> $word2, "\n";
print "When sorted: ", sort {$a <=> $b} ($word1, $word2), "\n";

@nlist = qw{109 2 21 107 10 219 32 1141};

print "Entire list in raw form:\n", @nlist, "\n";
print "Entire list in sorted form:\n", (sort {$a <=> $b} @nlist), "\n";

#END OF compare3.pl

(a). What happens when you put a , after the comparison block in sort?
(Revise and then run compare3.pl again.)



IF IT DOESN'T WORK:

1. In Question 4(e), one solution would be:
@array = qw{one two three};
print (@array**2), "\n";       
# should print 9 since 3 to the 2nd power is 9.

2. In Question 5(c), The outcome does change since the qw{ } operator
contains literal values, so this isn't the best way to specify a list.

Questions about the questions? Send mail to Robert Katz: rkatz@ned.highline.ctc.edu
Last Update January 17, 2002