本公眾號【讀芯樹:duxinshu_PD】主要介紹數(shù)字集成電路物理設(shè)計相關(guān)知識,才疏學(xué)淺,如有錯誤,歡迎指正交流學(xué)習(xí)。
這是集成電路物理設(shè)計的第七個系列【腳本語言】的第十六篇文章,本篇文章主要介紹perl相關(guān)內(nèi)容:
01
—
特殊字符
print 'filename is: '. __FILE__ .'\n'; #打印當(dāng)前文件名字
print 'linenum is: '. __LINE__ .'\n'; #打印當(dāng)前行行號
print 'packagename is: '. __PACKAGE__ .'\n'; #打印包文件
02
—
數(shù)組
#數(shù)組以@開頭
@fruit=(apple, oriage, banana);
print '$fruit[0]\n'; #打印apple
@fruit[3]=peach; #向數(shù)組添加元素
@num=(0..9); #定義數(shù)組0,1,2,...,9
@alp=(a..z); #定義數(shù)組a,b,c,...,z
print '@alp\n' #打印數(shù)組
print '@alp[4..8]' #打印第4,5,6索引的數(shù)值,0_based
print '', scalar @alp, '\n' #數(shù)組大小
$size=@alp; #數(shù)組物理大小,不是元素個數(shù)
$max_index=$#alp; #數(shù)組最大索引
push(@arr,value); #在arr結(jié)果加入value
pop(@arr); #刪除arr最后一個值
shift(@arr); #彈出第一個值
unshit(@arr, value); #在第一個位置加入value,順位后面所有元素
splice(@arr, offset, length, newlist) #替換從offset開始,長度為length的數(shù)值為newlist
@arr=split('-', '2022-05-18'); #將2022-05-18拆分為2022 05 18
$str=join('/', @arr); #將2022 05 18合并為2022/05/18
@arr=qw(3 1 5 2 3 6);
@arr=sort(@arr); #排序
$[=1; #設(shè)置第一個索引值為1
@str3=(1, 2, 3, (4, 5, 6)); #合并兩個數(shù)組
@list=(a, b, c, d ,e, f, g)[3..5]; #輸出3 4 5的索引值d, e, f
03
—
hash
hash是key:value對的集合,以%開頭
%week = ('1', 'Monday', '2', 'Tuesday', '3', 'Wednesday');
%week = ('1'=>'Monday', '2'=>'Tuesday', '3'=>'Wednesday');
print '\$week{'1'} = $week{'1'}\n';
@keys=keys %week;
print '@keys\n'; #返回hash所有的keys
@value=values %week;
print '@value\n'; #返回hash所有的values
if (exists($week{'3'})) {print '$week{'3'}'} else {print 'no this keys'}
@size=keys %week;
$num=@size; #hash大小
$week{'4'}='Thursday'; #添加新的keys:value
delete $week{'3'}; #刪除keys的value
foreach $key (keys %week) {print '$week{$key}\n';} #foreach循環(huán)
while (($key, $value)=each(%week)) {print '$week($key)\n'} #while循環(huán)
04
—
條件語句
#if-else
if (boolean_expression) {
body1
}else {
body2
}
#if-elseif-else
if (boolean_expression) {
body1
}elsif {
body2
}else{
body3
}
#unless
unless (boolean_expression) {
body #布爾表達(dá)式為非時執(zhí)行
}
#switch
switch (argument) {
case 1 {print '1'}
case a {print 'a'}
else {print 'other'}
}
#三元運(yùn)算符
$result={$a>10}? 'a > 10' : 'a <=10'; #$a>10,則選擇:左側(cè),否則選擇右側(cè)
05
—
循環(huán)
#while循環(huán)
$a=0;
while ($a<10) {
print '$a\n';
$a=$a+1;
}
#until循環(huán)
$a=0;
until ($a>9) {
print '$a\n';
$a=$a+1;
}
#for循環(huán)
for ($a=0; $a<10;$a=$a+1) {
print '$a\n';
}
#foreach循環(huán)
foreach $a (@list=(0..9)) {
print '$a\n';
}
#do-while循環(huán)
$a=0;
do {
print '$a\n';
$a=$a+1;
} while ($a<10)
#next語句
$a=0;
while ($a<10) {
if {$a==5} {
$a=$a+1;
next;
}
print '$a\n';
$a=$a+1;
}
#last語句
$a=0;
while ($a<10) {
if {$a==5} {
$a=$a+1;
last;
}
print '$a\n';
$a=$a+1;
}
#continue語句
$a=0;
while ($a<10) {
print '$a\n';
} continue {
$a=$a+1;
}
#redo語句
$a = 0;
while($a < 10){
if( $a == 5 ){
$a = $a + 1;
redo;
}
print 'a = $a\n';
}continue{
$a = $a + 1;
}
#goto語句
$a = 0;
LOOP:do
{
if( $a == 5){
$a = $a + 1;
goto LOOP;
}
print '$a\n';
$a = $a + 1;
}while( $a < 10 );
06
—
參考文獻(xiàn)
https://www.runoob.com/perl/perl-tutorial.html
https://www.perl.org/