1、perl脚本
open IN, "<", "data.txt" or die "The file does not exist!"; $line = 0; # 文本总的行数 while(<IN>){ chomp; #print("$_ "); push(@line_array,$_); # 将文本所有行存入数组 if(/^ALU/){ push(@ALU_index,$line); # 记录ALU所在行 } if(/^RAM/){ push(@RAM_index,$line); # 记录RAM所在行 } $line++; } #print("$line "); #print("@RAM_index "); foreach (@line_array){ print("$_ "); @elements = split(/s+/,$_); #按空格进行切分 push(@name_array,$elements[0]); #将每行的4个元素进行归类 push(@number_array,$elements[1]); push(@area_array,$elements[2]); push(@power_array,$elements[3]); } #print("@power_array "); # 计算总面积 $total_area = 0; for($i=1;$i<$line;$i++){ $total_area = $total_area + $number_array[$i]*$area_array[$i]; } print("total area is $total_area "); # 计算RAM的面积 $RAM_area = 0; foreach(@ALU_index){ $RAM_area = $RAM_area + $number_array[$_]*$area_array[$_]; } print("RAM area is $RAM_area "); $RAM_area_percentage = $RAM_area/$total_area*100; print("The percentage of RAM area is $RAM_area_percentage % "); close IN;
2、输出
Name: Instance_number Area-per-instance Power-per-instance ALU-Adder: 32 945 0.0333 ALU-Multiplier 16 6347 0.2235 RAM-Small 8 2239 0.0542 RAM-Large 2 10943 1.9312 Control 1 8345 0.2344 Data-pipe 1 10123 1.3423 Processor 1 24231 1.0212 total area is 214289 RAM area is 131792 The percentage of RAM area is 61.5019903028154 %
3、涉及知识点
1)数组的基本操作:push
2)按照空白字符进行分割:split(/s+/,$_),+表示多个空格
3)循环语句:for,foreach