收集了几个perl常用的基础函数,供大家学习参考。
#!/usr/bin/perl -w
use strict;
my $decimal = hex("af"); #十六进制转换为十进制
print "$decimaln";
my $rand_num = rand(4); #求0-4以内的随机数
print "$rand_numn";
my $rand_int = int(rand(4)); #0-4之间的随机整数
print "$rand_intn";
my ($sec,$min,$hour,$mday,$mon,
$year,$wday,$yday,$isdist) = localtime(time); #localtime(time)
#返回9个时间元素
print "$hour:$min:$secn";
my @file_stat = stat("found.pl"); #返回12个值
print "@file_statn";
#数组--grep函数
my @array = qw("google" "domain" "hello" "pen");
print "@arrayn";
my @filter = grep(/o/,@array);
print "@filtern";
#数组
(my $first_elem) = @filter; #把数组的第一个元素赋值给变量
print "$first_elemn";
#哈希表--delete
my %hash = (
name=> "Tou",
age=> "24",
address=> "GuangZhou"
);
#删除key为name的元素
delete $hash{"name"};
foreach (keys %hash){
my $value = $hash{$_};
print "$_ => $valuen";
}
#split函数
my $str = "China-Is-Greatful!";
my @splits = split(/-/,$str);
print "@splitsn";
#reverse 函数,反转
my @rev = reverse(@splits);
print "@revn";
#sort 函数,排序
my @numbers = qw(1 3 2 34 21 0 2);
my @sort_num = (sort{$a <=> $b} @numbers);
print "@sort_numn";
#length 函数,求长度
print length($str),"n";
#substr 函数,抽提字串
print substr($str,2,4),"n";
#index 函数,查找字符串第一次出现的位置
print index($str,"ea",5),"n";
#join 函数,在数组元素之间加入指定字符串
print join(" <= ",@sort_num),"n";