Perl 基础语法及数据结构

2023-10-21 14:19:57 浏览数 (1)

我也算学了基本脚本语言的人,Au3、Pyhon、PHP、JavaScript、Shell 都用过,但这个家伙是有点让我膛目,语法简直简练到不能再简练了。一下午的时间看了看他的基础语法和示例,做一下记录。

代码语言:javascript复制
#!perl

# 这是一行 Perl 的注释
print "Hello Perl";

=pod
这是 perl 的多行注释
这是 perl 的多行注释
这是 perl 的多行注释
=cut

# 基础变量
$var1 = 123456;     # 十进制
$var2 = 047;        # 八进制
$var3 = 0x1f;       # 十六进制

# 浮点型,不是非常精准
$var4 = 9.01e 21   0.01 - 9.01e 21;
print ("first value is ", $var4, "n");

# 字符串中变量替换,仅支持在双引号中使用
$number = 11;
$text = "This text contains the number $numbern";
print $text;

# 单引号可以跨多行,但不能使用转移和变量替换
$text1 = "This is two
lines of text
";
print $text1;

# 字符串和数值互相转换
$string = "43";
$number = 100;
$result = $string   $number;    # $result = 71
print $result;

$result = "Hello" * 5;          # $result = 0
print ("Hello * 5 = ", $result, "n");
$result = "12a34"   1;          # $result = 13
print ("12a34   1 = ", $result, "n");

# 字符串比较和逻辑操作符
if ($text eq $text1) {
    print "text = text1!n";
} elsif ($var1 > $var2) {
    print "$var1 > $var2! n";
} else {
    print "error!";
}

# 字符串自增
$stringvar = "abc";
$stringvar  ;
print $stringvar;       # print abd
$stringvar = "abcZZZ";
$stringvar  ;
print $stringvar;       # print abdAAA
print "n";

# 字符串拼接
$newstring = "potato" . "head";
print $newstring, "n";     # potatohead
$newstring = "t" x 5;
print $newstring, "n";     # ttttt

# 逗号运算符
$val = 26;
$result = (  $val, $val   5);   # $result = 32
print $result, "n";

# 数组变量
@array = (1, 2, 3);
$scalar = $array[0];
$array[3] = 5;          # now @array is (1,2,3,5)
$scalar = $array[4];    # now $scalar = null
$array[6] = 17;         # now @array is (1,2,3,5,"","",17)

# 用数组给列表赋值
@list1 = (2, 3, 4);
@list2 = (1, @list1, 5);# @list = (1,2,3,4,5)

# 用数组给普通多个变量赋值
@array = (5, 7, 11);
($var1, $var2) = @array;    # $var1 = 5, $var2 = 7, 11 被忽略
print ("var1 = $var1, var2 = $var2n");

@array = (5, 7);
($var1, $var2, $var3) = @array; # $var1 = 5, $var2 = 7, var3 = ""
print ("var1 = $var1, var2 = $var2, var3 = $var3n");

# 字符串中的方括号和变量替换
print ("$array[0] = $array[0]n");

# 列表范围
@number = (1..10);
print "@numbern";
@day_of_month = ("01".."31");
print "@day_of_monthn";
$fred = "Fred";
print (("Hello, " . $fred . "!n") x 2);

# 子数组
@array = (1, 2, 3, 4, 5);
@subarray = @array[0, 1];           # @subarray = (1, 2)
@subarray2 = @array[1..3];          # @subarray2 = (2, 3, 4)
@array[0, 1] = ("string", 46);      # @array = ("string", 46, 3, 4, 5)
@array[0..3] = (11, 22, 33, 44);    # @array = (11, 22, 33, 44, 5)
@array[0..2] = @array[3, 4];        # @array = (44, 5, "", 44, 5)
print "@arrayn";
@array[1, 2] = @array[2, 1];        # 可用于交换元素
print "@arrayn";

# 数组的库函数
@array = ("this", "is", "a", "test");
@array2 = sort(@array);
print "@array2n";

@array2 = reverse(@array);
print "@array2n";
@array2 = reverse sort (@array);
print "@array2n";

@list = ("rabbit", "12345", "quertz");  # rabbi 1234 quert
chop (@list);
print "@listn";

$string = join("", "My", "name", "is", "Jia Deng");
print "$stringn";  # MynameisJia Deng

@list = ("words", "and");
$string = join("::", @list, "colons");
print "$stringn";  # words::and::colons

@spt = split("::", $string);
print "@sptn";     # words and colons

# 条件判断
if ( "aa" eq "aa" ) {
    print "==n";
} else {
    print "!=n";
}

# 循环
$idx = 1;
while ($idx <= 100) {
    print "$idxt";
    $idx  ;
}
print "n";

until ($idx <= 1) {
    print "$idxt";
    $idx--;
}
print "n";

# 类 C 语言 break 和 continue
$i = 0;
while ($i < 100) {
    if ($i == 20) {
        # 到 20 停止循环
        last;
    }
    print "$it";
    $i  ;
}
print "n";

$i = 0;
while ($i < 30) {
    if ($i == 20) {
        # 将跳过 20 
        $i  ;
        next;
    }
    print "$it";
    $i  ;
}
print "nn";

# for
@list = (1, 2, 3, 4, 5);
foreach $temp (@list) {
    if ($temp == 2) {
        $temp = 20;
    }
    print "$tempt";
}
print "n";

# 单行条件
print ("@list length = " . @list . "n")   if (@list < 10);

# 关联数组
%fruit = ("apples" => 17, "bananas" => 9, "oranges" => 100);
foreach (keys(%fruit)) {
    print ("$_ = $fruit{$_}t");
}
print "n";

# 删除一个关联数组的元素
delete $fruit{apples};
foreach (keys(%fruit)) {
    print ("$_ = $fruit{$_}t");
}
print "n";

# each() 遍历关联数组
%records = ("Maris", 61, "Aaron", 755, "Young", 511);
while (($key, $value) = each(%records)) {
    print ("$key => $valuet");
}

0 人点赞