📄 ch3.htm
字号:
<HTML>
<HEAD>
<TITLE>Chapter 3 -- Variables</TITLE>
<META>
</HEAD>
<BODY TEXT="#000000" BGCOLOR="#FFFFFF" LINK="#0000EE" VLINK="#551A8B" ALINK="#CE2910">
<H1><FONT SIZE=6 COLOR=#FF0000>Chapter 3</FONT></H1>
<H1><FONT SIZE=6 COLOR=#FF0000>Variables</FONT></H1>
<HR>
<P>
<CENTER><B><FONT SIZE=5>CONTENTS</FONT></B></CENTER>
<UL>
<LI><A HREF="#ScalarVariables">
Scalar Variables</A>
<UL>
<LI><A HREF="#ExampleAssigningValuestoScalarVariables">
Example: Assigning Values to Scalar Variables</A>
<LI><A HREF="#ChangingValuesinScalarVariables">
Changing Values in Scalar Variables</A>
</UL>
<LI><A HREF="#ArrayVariables">
Array Variables</A>
<UL>
<LI><A HREF="#ExampleAssigningValuestoArrayVariables">
Example: Assigning Values to Array Variables</A>
<LI><A HREF="#ExampleUsingArrayElements">
Example: Using Array Elements</A>
<LI><A HREF="#ExampleUsingNegativeSubscripts">
Example: Using Negative Subscripts</A>
<LI><A HREF="#ExampleDeterminingtheNumberofElementsinanArray">
Example: Determining the Number of Elements in an Array</A>
<LI><A HREF="#ExampleHowtoGrabaSliceorPartofanArray">
Example: How to Grab a Slice (or Part) of an Array</A>
</UL>
<LI><A HREF="#AssociativeArrayVariables">
Associative Array Variables</A>
<UL>
<LI><A HREF="#ExampleAssigningValuestoAssociativeArrayVariables">
Example: Assigning Values to Associative Array Variables</A>
</UL>
<LI><A HREF="#DoubleQuotedStringsRevisited">
Double-Quoted Strings Revisited</A>
<UL>
<LI><A HREF="#ExampleVariableInterpolation">
Example: Variable Interpolation</A>
<LI><A HREF="#ExampleUsingtheIquotISpecialVariable">
Example: Using the <I>$"</I> Special Variable</A>
</UL>
<LI><A HREF="#Summary">
Summary</A>
<LI><A HREF="#ReviewQuestions">
Review Questions</A>
<LI><A HREF="#ReviewExercises">
Review Exercises</A>
</UL>
<HR>
<P>
In the last chapter, you learned about <I>literals</I>-values
that don't change while your program runs because you represent
them in your source code <I>exactly</I> as they should be used.
Most of the time, however, you will need to change the values
that your program uses. To do this, you need to set aside pieces
of computer memory to hold the changeable values. And, you need
to keep track of where all these little areas of memory are so
you can refer to them while your program runs.
<P>
Perl, like all other computer languages, uses variables to keep
track of the usage of computer memory. Every time you need to
store a new piece of information, you assign it to a variable.
<P>
You've already seen how Perl uses numbers, strings, and arrays.
Now, you'll see how to use variables to hold this information.
Perl has three types of variables:<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD WIDTH=151><I>Variable Type</I></TD><TD WIDTH=439><I>Description</I>
</TD></TR>
<TR><TD WIDTH=151>Scalars</TD><TD WIDTH=439>Holds one number or string value at a time. Scalar variable names always begin with a <TT>$</TT>.
</TD></TR>
<TR><TD WIDTH=151>Arrays</TD><TD WIDTH=439>Holds a list of values. The values can be numbers, strings, or even another array. Array variable names always begin with an <TT>@</TT>.
</TD></TR>
<TR><TD WIDTH=151>Associative Arrays</TD><TD WIDTH=439>Uses any value as an index into an array. Associative array variable names always begin with an .
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The different beginning characters help you understand how a variable
is used when you look at someone else's Perl code. If you see
a variable called <TT>@Value</TT>,
you automatically know that it is an array variable.
<P>
They also provide a different <I>namespace</I> for each variable
type. Namespaces separate one set of names from another. Thus,
Perl can keep track of scalar variables in one table of names
(or namespace) and array variables in another. This lets you use
<TT>$name</TT>, <TT>@name</TT>,
and <TT>%name</TT> to refer to different
values.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
I recommend against using identical variable names for different data types unless you have a very good reason to do so. And, if you do need to use the same name, try using the plural of it for the array variable. For example, use <TT>$name</TT> for the
scalar variable name and <TT>@names</TT> for the array variable name. This might avoid some confusion about what your code does in the future.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Variable names in Perl are case-sensitive. This means that <TT>$varname</TT>, <TT>$VarName</TT>, <TT>$varName</TT>, and <TT>$VARNAME</TT> all refer to different variables.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Each variable type will be discussed in its own section. You'll
see how to name variables, set their values, and some of the uses
to which they can be put.
<H2><A NAME="ScalarVariables"><FONT SIZE=5 COLOR=#FF0000>
Scalar Variables</FONT></A></H2>
<P>
Scalar variables are used to track single pieces of information.
You would use them to hold the title of a book or the number of
rooms in a house. You can use just about any name imaginable for
a scalar variable as long as it begins with a <TT>$</TT>.
<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
If you have programmed in Visual Basic, you need to be especially careful when naming variables. Just remember that <I>all</I> scalars begin with a <TT>$</TT>, not just strings, and that the <TT>$</TT> starts the name; it doesn't end it.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Let's jump right in and look at some variable names.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>This scalar variable will hold the number of rooms.<BR>
This scalar variable will hold the title of a book.<BR>
This scalar variable conflicts with a Perl special variable that
you'll learn about in <A HREF="ch12.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch12.htm" >Chapter 12</A>, "Using Special Variables."</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$numberOfRooms
$bookTitle
$0<BR>
</PRE>
</BLOCKQUOTE>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
It is generally a good idea to stay away from short variable names. Longer variable names are more descriptive and aid in understanding programs.</BLOCKQUOTE>
<BLOCKQUOTE>
Let me say a quick word about variable names. I always start my variable names with a lowercase letter and then make the first letter of each "word" in the name uppercase. Some programmers like to separate each word with an underscore. For
example, <TT>$numberOfRooms</TT> would look like <TT>$number_of_rooms</TT>. Choose a method that you feel comfortable with and then stick with it. Being consistent will make your program more understandable.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
Most programmers try to use descriptive names for their variables.
There is no practical limit to the length of a Perl variable name,
but I like to keep them under 15 characters. Anything longer than
that means that it will take a while to type them and iNCreases
the chaNCes of spelling errors.
<H3><A NAME="ExampleAssigningValuestoScalarVariables">
Example: Assigning Values to Scalar Variables</A></H3>
<P>
Now that you know what scalar variable names look like, we'll
look at how you can assign a value to them. Assigning values to
a variable is done with the equals (=) sign.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Assign a value of 23 to a variable called </I><TT><I>$numberOfRooms</I></TT><I>.
<BR>
Assign a value of Perl by Example to a variable called </I><TT><I>$bookTitle</I></TT><I>.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$numberOfRooms = 23;
$bookTitle = "Perl by Example";
</PRE>
</BLOCKQUOTE>
<P>
Notice that you are assigning literal values to the variables.
After assigning the values, you then can change them.
<H3><A NAME="ChangingValuesinScalarVariables">
Changing Values in Scalar Variables</A></H3>
<P>
The next example will make a variable assignment and then change
that variable's value using a second assignment. The second assignment
will iNCrement the value by five.
<P>
<IMG SRC="pseudo.gif" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/pseudo.gif" BORDER=1 ALIGN=RIGHT><p>
<BLOCKQUOTE>
<I>Assign a value of 23 to a variable called </I><TT><I>$numberOfRooms</I></TT><I>.
<BR>
Add 5 to the </I><TT><I>$numberOfRooms</I></TT><I>
variable.</I>
</BLOCKQUOTE>
<BLOCKQUOTE>
<PRE>
$numberOfRooms = 23;
$numberOfRooms = $numberOfRooms + 5;<BR>
</PRE>
</BLOCKQUOTE>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Note</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
In Perl, you never have to declare, define, or allocate simple data types (for example: scalars, arrays, or associative arrays). When you use the variable for the first time, Perl either assigns it a zero if you need a number or an empty list if you need
an array. Using a variable name is equivalent to defining it.</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Caution</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
Letting Perl automatically initialize variables is fine for small programs. However, if you write professional programs that need to be maintained, you'll want to explicitly declare variables using the <TT>my()</TT> fuNCtion. Explicitly declaring
fuNCtions will reduce errors and improve the internal documentation of your programs. The <TT>my()</TT> fuNCtion is discussed in <A HREF="ch5.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch5.htm" >Chapter 5</A> "FuNCtions."
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<H2><A NAME="ArrayVariables"><FONT SIZE=5 COLOR=#FF0000>
Array Variables</FONT></A></H2>
<P>
You had a short introduction to arrays last chapter when you printed
out entire arrays (with no spaces, remember?) using Perl's <TT>print
</TT>statement. Now, you'll learn about arrays in more
detail. Array variable names always begin with an <TT>@</TT>
character.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
I remember that the <TT>@</TT> sign starts array variables because "at" and "array" start with the same letter. Simple…but it works for me.
</BLOCKQUOTE>
</TD></TR>
</TABLE>
</CENTER>
<P>
<P>
The rules for naming array variables are the same as those for
scalar variables. There are no rules. Well, none that you need
to worry about. In fact, let's skip looking at variable names
and get right to assigning arrays to variables, instead.
<H3><A NAME="ExampleAssigningValuestoArrayVariables">
Example: Assigning Values to Array Variables</A></H3>
<P>
You use the equals (=) sign to assign values to array variables
just like scalar values.
<P>
We'll use one of the examples from <A HREF="ch2.htm" tppabs="http://cheminf.nankai.edu.cn/~eb~/Perl%205%20By%20Example/ch2.htm" >Chapter 2</A> "Numeric and
String Literals"-reworked a little-so you'll already be familiar
with part of the example.<BR>
<p>
<CENTER>
<TABLE BORDERCOLOR=#000000 BORDER=1 WIDTH=80%>
<TR><TD><B>Tip</B></TD></TR>
<TR><TD>
<BLOCKQUOTE>
The printing of the newline character is separated from the printing of the array for a reason. It has to do with how Perl interprets variables in different contexts. If you tried to use <TT>print @numberArray . "\n"</TT>; Perl thinks that you
want to use <TT>@numberArray</TT> in a scalar context and won't print the elements of the array. It will print the number of elements instead. See the section, "Example: Determine the Number of Elements in an Array," later in this chapter.
</BLOCKQUOTE>
</TD></TR>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -