本文内容:
- Perl里面的值与变量定义
- 变量的三种类型
- scalar 变量的应用
——————————————
很典型的是,我们希望通过使用Perl来处理一些数据,从而产生一些output。这也就意味着我们会有一系列的值并且把他们转变成其他的值,这些转变的值会更具有信息价值。在Perl里,我们会创建数据结构(data structure)来支撑我们的值,这样能够进行操纵和修改。我们称之为变量,因为我们可以改变它的值在处理分析的时候。
在Perl里主要有三种形式的数据结构(data structure):scalars:标量; arrays:阵列; hashes:碎片。scalar 是单独的值,比如numbers (数值)或者characters (字符)。一行字符在Perl里会计为一个单独的值。Array 是有排序的一列值(list of values)。Hashe就是没有排序的些值。变量的名字前面会根据不同的形式作以不同的标记,如下。
类型 | 符号 | 定义 |
---|---|---|
Scalar | $ |
single values |
Array | @ |
ordered lists of scalars |
Hash | % |
unordered collections of scalar |
值可以通过使用 =
来赋给变量。如下
在文本编辑器gedit里输入以下内容并且命名为,lesson_02.pl。
# use ‘my’ the First time you use a variable
my $serine =“TCA”;
my $codonLength = 3;
print “Codon $serine has $codonLength bases\n”;
实践:

在终端运行Perl interpreter:

我们也可以把变量的值进行改变,还是使用=
:
my $ serine = “TCA”;
print “Serine is coded by $serine\n”;
$serine = “TCC”;
print “Serine is also coded by $serine\n”;
实践

在终端运行Perl interpreter:

我们也可以使用赋值以后的变量,来给它本身进行新的赋值,例如(以下省略键入脚本部分,请直接参考截图):

在终端运行Perl interpreter:

另一个scalar赋值的例子:

在终端运行Perl interpreter:

标量 (Scalars):
Type of scalar | Examples |
---|---|
number: integers | 1 2 -3 128 |
number: floating-points | -0.2 0.1 3.14159 |
string: letters | “a” “A” “G” “c” |
string: words | “Protein” “DNA” |
string: words and spaces and punctuation | “DNA makes RNA makes Protein.” |
reference | (see later lessons) |
单引号和双引号(Single and Double Quotes)
如果你命名了一个变量为”$codon”
,在print里面必须使用双引号,才会把变量的值打印出来;如果用单引号,则会直接得到$codon
。这个也包括”\n”
。例子:

在终端运行Perl interpreter:

练习题(这个没有正确答案,我自己写的)
- Look up the genetic code. Create a number of scalar variables that store possible DNA encodings of serine (S), leucine (L), tyrosine (Y) and cysteine (C). Where multiple codings are available, just pick one for now.

在终端运行Perl interpreter:

- Create a variable containing a possible DNA sequence for the protein sequence SYLYC. Use the concatenation operator.

在终端运行Perl interpreter:

【此系列内容属于学习笔记】
版权©️: http://nbviewer.jupyter.org/github/grsr/perl-course/blob/master/Learning_Perl.ipynb
网友评论