Basic Lists and List Versus Scalar Context
```#!/usr/bin/perl -w
########  Basic Lists and the Idea of List Versus Scalar Context  #######

@list = (1,3,5,7,9);
print "\$list[2]\n";      #  Prints 5
@list = ("what", "when", "where");
print "\$list[1]\n";      #  Prints "when" (quotes not included).
@list = qw (what when where);
print "\$list[1]\n";      #  Prints "when" (quotes not included).

print "Hello, Earthling!\n" if @list > 2;  #  Print will happen!  @list is 3
#  in a scalar context!

print scalar(@list), "\n";   #  Force scalar context!
\$num_elements = @list;
print "\$num_elements\n";     #  Prints 3.

(\$a,\$b,\$c) = (1,2,"who");
print "\$c\n";     #  Prints "who" (without quotes!).

(\$a,\$b,\$c) = (1,2,3,4);
print "\$c\n";     #  Prints 3

(\$a,\$b,\$c) = (1,2);
if (defined(\$c)) {
print "\\$c is defined\n";
}
else {
print "\\$c is not defined!\n";    #  This should print!!
}

\$c = "";     #  Does this define \$c???
if (defined(\$c)) {
print "\$c is defined as empty string!\n";   #  This should print!!
}
else {
print "\\$c is not defined as empty string!\n";
}

####################  How to Make Lists Out of Scalars  ###################
#  The Split Function

\$var = "abc:def:ghi:jkl::::";
@list = split(/:/, \$var);    #  Trailing empty fields will be tossed!!
print scalar(@list), "\n";   #  Should be 4.
print "@list\n";

\$var = "abc:def:ghi:jkl::::";
@list = split(/:/, \$var, 10);    #  Trailing empty fields will NOT be tossed!!
print scalar(@list), "\n";       #  Should be 8.
print "@list\n";                 #  Last four fields will be empty strings!

\$var = "abc:def:ghi:jkl::::";
@list = split(/:/, \$var, 2);    #  Note that we're asking for fewer than we have
print scalar(@list), "\n";      #  Should be 8.
print "@list\n";                #  Second of the two fields will have colons!!
\$var = "abc::def::::ghi:jkl::::";
@list = split(/:+/, \$var);      #  Trailing empty fields will be tossed!!
print scalar(@list), "\n";      #  Should be 4.  Now, any number of colons is
print "@list\n";                #  just ONE delimiter.

\$var = "::abc::def:ghi:jkl";
@list = split(/:/, \$var);       #  Leading/embedded empty fields NEVER tossed!!
print scalar(@list), "\n";      #  Should be 7 -- three fields empty strings!
print "@list\n";

# Match-Global -- better than split when there are no predictable delimiters!

\$var = "\\$%45::3---100++*(75###";
@numbers = \$var =~ /\d+/g;   #  Get digit strings out of \$var. Put in @numbers.
print "@numbers\n";          #  Should be 45 3 100 75

\$var = "&^%order*()&in\\$#chaos\\$\%^\\$#is\%\%4&^\%the*&^good";
@words = \$var =~ /\w+/g;     #  Get "word-character" strings.
print "@words\n";

\$var = "123&^#**---56*";
@goofy = \$var =~ /[\d*]+/g;  #  Strings of any combo of digits and asterisks.
print "@goofy\n";

#  Match Tagged Fields -- good when the stuff you want to get are specific
#                         parts of a variable.

\$var = "(*&123#^&letters()&^345(*(stuff";
@num_and_word = \$var =~ /.*?(\d+).*?([a-z]+)/;   #  Get first digit string and
print "@num_and_word\n";                         #  then first letter string
#  after it.
#############################  Program Output  ###############################
5
when
when
Hello, Earthling!
3
3
who
3
\$c is not defined!
is defined as empty string!
4
abc def ghi jkl
8
abc def ghi jkl
2
abc def:ghi:jkl::::
4
abc def ghi jkl
7
abc  def ghi jkl
45 3 100 75
order in chaos is 4 the good
123 ** 56*
123 letters
########################  Lessons from This Code  #########################

1)  The value of a list in a scalar context is the number of elements in
the list.

2)  List indices start at zero.

3)  Conditionals of if's, unless's, and while's are a scalar context.

4)  Use qw(element1 element2 etc) to create lists of strings.  It is
easier on the eyes than @list = ("element1", "element2", "etc").

5)  The built-in split function can be used to create a list from a
string.  Split does NOT make list elements out of trailing empty
fields but leading or embedded empty fields are list elements.
Thus, if you have:

\$junk = ":::a:b::c:d::::";
@list = split(/:/,\$junk);
print scalar(@list);     #  Prints 8

then \$list[0], \$list[1], \$list[2], and \$list[5] are DEFINED empty
strings.  Remember, the split expression can be ANY legal regular
expression.  Thus:

\$junk = ":::a:b::c:d::::";
@list = split(/:+/,\$junk);   #  Notice the plus sign!
print scalar(@list);         #  Prints 5

creates @list with a total of only 5 fields because any number of
consecutive colons is ONE delimiter!!

6.  The expression:   @list = \$junk =~ /regular-expression/g;

Represents very, very powerful Perl magic.  The "g" at the end means
"match global".  It says: "go get every piece of \$junk matching the
regular expression and make a list element out of it".  This magic
will be explained in great detail in class!  Don't miss it!

7.  Use split to create lists when the text has very regular delimiters.
Use match-global to create lists when you want to get a certain type
of string in "chaotic" text (text without regular delimiters).
Use match-tagged-fields when you want to get very specific stuff off
of a line of text.
```