Perl-晋阶
2、Perl 晋阶:
1、perl 与正则表达式:
1、匹配操作符 m/ / :用于匹配一个字符串语句或者一个正则表达式,例如,要匹配 标量 $bar 中的 "run",代码如下所示:
$bar = "I am runoob site. welcome to runoob site."; if ($bar =~ /run/){ print "第一次匹配\n"; }else{ print "第一次不匹配\n"; } $bar = "run"; if ($bar =~ /run/){ print "第二次匹配\n"; }else{ print "第二次不匹配\n"; }
2、替换操作符 s/ / / :是匹配操作符的扩展,使用新的字符串替换指定的字符串。
$string = "welcome to google site."; $string =~ s/google/runoob/; print "$string\n";
3、转化操作符tr:
$string = 'welcome to runoob site.'; $string =~ tr/a-z/A-z/; print "$string\n";
2、perl 格式化输出:
3、perl 文件输入与输出:
open(FILE_IN, "<file.txt") or die "file.txt 文件无法打开, $!"; while(<FILE_IN>){ print "$_"; }
#打开文件并将文件内容输出:
<表示只读方式。>表示写方式。
代码中的 FILE_IN为文件句柄用于读取文件
open(FILE_OUT, ">file.txt") or die "file.txt 文件无法打开, $!";
close(FILE_OUT) || die "无法关闭文件";
4.在IC设计及验证过程中,经常会遇到mem初始化的问题,这时候需要产生hex 的文件,本程序实现这种需求,只需要输入行数,及hex文件的宽度即可。
print"Hello World!\n"; $file_out = ">My_Hex.dat"; open(FILE_OUT,$file_out) || die "file not open"; print("Input the line num : \n"); my $line_cnt = <STDIN> ; print("Input the Hex num (one hex is half byte): \n"); my $byte2_cnt = <STDIN> ; my $my_hex = qw//; my @array_hex = qw/0 1 2 3 4 5 6 7 8 9 a b c d e f/; while($line_cnt){ for(my $i=0; $i<$byte2_cnt; $i++){ #产生一个0-16的随机数,用来从@array_hex 中取出对应的hex。 my $j = (int rand(16)) %17 ; #.号可以拼接操作,另外的一种拼接是用双引号。 #$my_hex = “$my_hex$array_hex[$j]”; $my_hex = $my_hex.$array_hex[$j]; } print FILE_OUT ($my_hex,"\n"); $my_hex = qw//; $line_cnt--; } close FILE_OUT; print"END Hello World\n";
程序执行结果:
本文来自博客园,作者:hematologist,转载请注明原文链接:https://www.cnblogs.com/littleMa/p/5325802.html
posted on 2016-03-27 15:28 hematologist 阅读(435) 评论(0) 编辑 收藏 举报