1、join函数 #!/usr/bin/perl @array = (1,2,3,4,5,6); print join(",",@array); 2、vec函数 #!/usr/bin/perl # 16进制数字转为二进制数字 $hexdigit = 0xA; vec($data,8) = $hexdigit; print vec($data,1); print vec($data,1,1); 3、abs函数 绝对值 #!/usr/bin/perl $s = -5; print "The absolute value of $s = ",abs $s; 4、atan2函数 反正切 #!/usr/bin/perl print (4 * atan2 1,1); 5、Math::BigInt和Math::BigFLoat函数 大数 扩展算数的精确度 #!/usr/bin/perl use Math::BigInt; $bi = Math::BigInt->new('11111111111111111111'); print $bi * $bi; 6、chr函数 字符码中的字符 #!/usr/bin/perl foreach (65 .. 68) { print chr()," "; } 7、Math::Complex函数 复数 #!/usr/bin/perl use Math::Complex; $c1 = Math::Complex->new(-2,3); $c2 = Math::Complex->new(4,5); $c3 = $c1 * $c2; print "($c1) X ($c2) = $c3\n"; 8、cos函数 余弦 #!/usr/bin/perl # 45度转化为弧度后再计算余弦值 $angle = 45; $conversion = 3.14159265358979 / 180; $radians = $angle * $conversion; print "The cosine of $angle degrees = ",cos $radians; 9、each函数 hash表键/值 对 #!/usr/bin/perl # each 返回键值对表 $hash{sandwich} = grilled; $hash{drink} = 'root beer'; while (($key,$value) = each(%hash)) { print "$key => $value\n"; } 10、eval函数 运行期间计算Perl代码 #!/usr/bin/perl eval {print "Hello "; print "there.";}; #!/usr/bin/perl # eval 将报告错误 sub try(&) { my $code = shift; eval {&$code}; if($@) {print "eval says: $@";} }; try{ $operand1 = 1; $operand2 = 0; $result = $operand1 /$operand2; }; 11、exists函数 检查hash表键,仅指出hash表是否存在某个键 #!/usr/bin/perl $hash{ID} = 12334; $hash{Name} = Bertie; $hash{Division} = Sales; if (exists($hash{Phone})) { print "Key is in the hash."; } else { print "Key is not in the hash."; } 要真正确定是否定义了哈希表中的某个元素,用defind #!/usr/bin/perl $hash{ID} = 12334; $hash{Name} = Bertie; $hash{Division} = Sales; if (defined($hash{Phone})) { print "Element is defined."; } else { print "Element is not defined."; } 12、exp函数 计算e的幂 #!/usr/bin/perl # 让用户输入幂次数 print "Welome to the Exponentiator!\n"; print "Enter a number: "; while ($s = <>) { print "\n"; print " $s"; print "e = " . exp($s) . "\n"; print "Enter a number: "; } 13、grep函数 查找匹配函数 #!/usr/bin/perl # 删除由4个字母构成的单词 print join(" ",(grep{!/^\w{4}$/} (qw(Here are some four letter words.)))); 14、hex函数 16进制转换 #!/usr/bin/perl print hex("10") . "\n"; print hex("0x10") . "\n"; print hex("ab") . "\n"; print hex("Ab") . "\n"; 15、index函数 子串的位置 #!/usr/bin/perl $text = "Here's the text!"; print index $text,'text' 16、int函数 截断整数 #!/usr/bin/perl print int 1.999; print "\n"; print int 2.001; print "\n"; 17、integer模块 整数计算 #!/usr/bin/perl # 从$value 数字中剥离连续的16进制数字,从而将那个数字转换为16进制数字 use integer; $value = 258; print "$value in hex = "; while($value) { push @digits,(0 .. 9,a .. f)[$value & 15]; $value /= 16; } while(@digits){ print pop @digits; } 18、join函数 将表加入到字符串中 #!/usr/bin/perl @array = (1,6,7,8,9); print join(",@array) . "\n"; 19、keys函数 得到hash表键 #!/usr/bin/perl # $hash{sandwich} = salami; $hash{drink} = 'root beer'; foreach $key (keys %hash) { print $hash{$key} . "\n"; } #!/usr/bin/perl # 哈希表的键数量 $hash{sandwich} = salami; $hash{drink} = 'root beer'; print "\%hash has " . keys(%hash) . " keys\n"; 20、lc函数 转换为小写 lu转为大写 #!/usr/bin/perl while(<>) { print "Here's what you typed lowercased: " . lc($_) . "\n"; print "Here's what you typed uppercased: " . uc($_) . "\n"; } 21、lcfirst函数 第一个字符转换为小写 ucfirst第一个字符转换为大写 #!/usr/bin/perl print lcfirst "I like poems by e.e. cummings."; 22、length函数 得到字符串的长度 #!/usr/bin/perl $text = "Here is the text."; print length $text; 23、pack函数 将值打包到字符串中 #!/usr/bin/perl print pack("ccc",88,89,90) . "\n"; print pack("c3",65,66,67) . "\n"; print pack("c*",68,69,70,71) . "\n"; #!/usr/bin/perl # 将数字转换为二进制构成的字符串 $decimal = 100; $binary = unpack("B32",pack("N",$decimal)); print $binary; #!/usr/bin/perl # 将二进制构成的字符串转换为数字 $decimal = 100; $binary = unpack("B32",$decimal)); $newdecimal = unpack("N",pack("B32",$binary)); print $newdecimal; 24、rand函数 创建随机数 #!/usr/bin/perl $random = rand(100); print $random; #!/usr/bin/perl $letter = ('a' .. 'z')[26 * rand]; print $letter; #!/usr/bin/perl print "Some lottery numbers to try:"; foreach (1 .. 6) { print " " . int rand(50) + 1; } 25、reverse函数 颠倒表 #!/usr/bin/perl print join(" ",reverse(1 .. 20)); #!/usr/bin/perl @array = (1,3); print join(",reverse @array); #!/usr/bin/perl $hash{sandwich} = grilled; $hash{drink} = 'root beer'; %reversed = reverse %hash; while($key = each(%reversed)) { print "$key => $reversed{$key}\n"; } #!/usr/bin/perl $string = "Hello!"; $reversed = reverse($string); print "$reversed\n"; 26、rindex函数 颠倒索引 #!/usr/bin/perl $text = "I said,no,I just don't kNow."; print "First occurrence of \"no\" is at position: " . index ($text,"no") . "\n"; print "Last occurrence of \"no\" is at position: " . rindex($text,"no") . "\n"; 27、sin函数 正弦 #!/usr/bin/perl $angle = 45; $conversion = 3.14159265358979 / 180; $radians = $angle * $conversion; print "The sine of $angle degrees = ",sin $radians; 28、sort函数 排序表 #!/usr/bin/perl @array = ('z','b','a','x','y','c'); print join(",@array) . "\n"; print join(",sort{$a cmp $b} @array) . "\n"; print join(",sort{$b cmp $a} @array) . "\n"; @array = (1,2); print join(",sort{$a <=> $b} @array) . "\n"; print join(",sort{$b <=> $a} @array) . "\n"; 29、split函数 字符串拆分为字符串数组 #!/usr/bin/perl print join('-',split(//,'Hello')); print "\n"; print ((split " ","Now is the time")[3]); print "\n"; 30、sprintf函数 格式化字符串 #!/usr/bin/perl $value = 1234.56789; print sprintf "%.4f\n",$value; print sprintf "%.5f\n",$value; print sprintf "%6.6f\n",$value; print sprintf "%+.4ef\n",$value; 31、sqrt函数 平方根 #!/usr/bin/perl print "Welcome to the Hypotenusizer!\n"; print "Enter two sides of a right triangle: "; while(<>) { ($a,$b) = split; $hypotenuse = sqrt($a * $a + $b * $b); print "The hypotenuse is: ",$hypotenuse,"\n"; print "Enter two sides of a right triangle: "; } 32、srand函数 设置随机数种子 #!/usr/bin/perl srand; $random = rand(100); print $random; 33、substr函数 #!/usr/bin/perl $text = "Here is the text."; print substr($text,12) . "\n"; print substr($text,12,4) . "\n"; substr($text,"word"); print "$text\n"; 34、time函数 得到1970年1月1日以来的秒数 #!/usr/bin/perl print "Current epoch time in seconds = ",time,"\n"; print "Current time = ",scalar localtime(time()),"\n"; 35、Math::Trig函数 三角函数 #!/usr/bin/perl use Math::Trig; print "Pi = ",pi,"\n"; print "Pi in degrees = ",rad2deg pi,"\n"; print "The tangent of 0 = ",tan(0),"\n"; print "The arccos of 1= ",acos(1),"\n"; print "The arcsin of 1 / sqrt(2) = ",rad2deg(asin(1/sqrt(2)))," degrees\n"; 36、values函数 得到hash表值 #!/usr/bin/perl $hash{sandwich} = 'ham and cheese'; $hash{drink} = 'diet cola'; foreach $value (values %hash) { print "$value\n"; } 37、vec函数 访问无符号整数向量 #!/usr/bin/perl # 得到数据中的每位,以二进制形式显示16进制数据 $hexdigit = 0xA; vec($data,8) = $hexdigit; print vec($data,1); POSIX函数
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。