Michael's
2025年3月25日 星期二
MultiPlot
set multiplot layout 2,1 rowsfirst #上下兩張圖 先橫再直
set multiplot layout 2,1 columnsfirst #上下兩張圖 先直再橫
set multiplot layout 1,2 rowsfirst #左右兩張圖
set multiplot layout 2,3 title "Multi 6 Plot"
多個獨立視窗
# 第一張圖
set term qt 0
plot sin(x)
# 第二張圖
set term qt 1
plot cos(x)
2025年2月20日 星期四
計算table裡面的最大值 最小值 平均值
#!/usr/bin/perl
use strict;
use warnings;
#檔案路徑 (請自行修改成正確路徑)
my $filename = $ARGV[0];
my @numbers;
#檢查檔案是否存在
unless (-e $filename) {
die "檔案 $filename 不存在! \n";
}
#讀取檔案內容
open (DATA, "<$filename");
while (){
chop $_;
$_ =~ s/[",\\();a-zA-Z]//g;
print "$ \n";
my @ary=split(" ",$_);
push @numbers, @ary;
}
## 計算最大值、最小值和平均值
my $max = (sort { $b <=> $a } @numbers)[0];
my $min = (sort { $a <=> $b } @numbers) [0];
my $sum =0;
$sum += $_ for @numbers;
my $avg = $sum / @numbers;
## 輸出結果
print "最大值: $max\n";
print "最小值: $min\n";
print "平均值: $avg\n";
close (DATA);
使用方式 perl max_min_avg.pl riseC
結果:
最大值:0.00323131
最小值: 0.00226739
平均值:0.002983495
riseC:
"0.00264617, 0.00237069, 0.00230644, 0.00228424, 0.00227416, 0.00227032, 0.00226836, 0.00226739",
"0.00322831, 0.00323131, 0.00316939, 0.00283283, 0.0025619, 0.00242233, 0.00235136, 0.002317", \
"0.00322562, 0.00322585, 0.00322684, 0.00321483, 0.00300823, 0.0026767, 0.00248122, 0.00238",
"0.00322181, 0.00322257, 0.0032231, 0.00322229, 0.00322081, 0.00306827, 0.00272794, 0.00250753",\
"0.00321706, 0.00321843, 0.00321988, 0.00321986, 0.0032225, 0.0032165, 0.0030942, 0.00275446",\
"0.00321555, 0.00321851, 0.00321821, 0.00321952, 0.00322031, 0.00322318, 0.00321586, 0.00310252",
"0.00321461, 0.00321901, 0.00321834, 0.00321678, 0.00321843, 0.00322075, 0.00321996, 0.00321428",
"0.00321823, 0.00321797, 0.00321929, 0.0032173, 0.00321809, 0.00321879, 0.00321988, 0.00321961"
2025年1月19日 星期日
保留pin RETN{} block 跟mega_ 參數
#!/usr/bin/perl
use strict;
use warnings;
# 檔案路徑
my $input_file = 'input.txt'; # 請將 'input.txt' 替換為你的檔案名稱
my $output_file = 'output.txt';
# 讀取檔案內容
open my $in_fh, '<', $input_file or die "無法打開檔案 $input_file: $!";
my @lines = <$in_fh>;
close $in_fh;
# 處理檔案內容
my $inside_block = 0;
my @filtered_lines;
foreach my $line (@lines) {
if ($line =~ /pin \("RETN"\) \{/) {
$inside_block = 1; # 開始進入區塊
}
if ($inside_block || $line =~ /mega_/) {
push @filtered_lines, $line;
}
if ($inside_block && $line =~ /xRxFxxx/) {
$inside_block = 0; # 結束區塊
}
}
# 寫入處理後的內容到新檔案
open my $out_fh, '>', $output_file or die "無法創建檔案 $output_file: $!";
print $out_fh @filtered_lines;
close $out_fh;
print "已處理完成,結果存入 $output_file\n";
計算altos_veclist裡面有幾個vector並把數量寫在下面
#!/usr/bin/perl
use strict;
use warnings;
# 檔案路徑
my $input_file = 'input.txt'; # 請將 'input.txt' 替換為你的檔案名稱
my $output_file = 'output.txt';
# 讀取檔案內容
open my $in_fh, '<', $input_file or die "無法打開檔案 $input_file: $!";
my @lines = <$in_fh>;
close $in_fh;
# 處理檔案內容
my @updated_lines;
foreach my $line (@lines) {
push @updated_lines, $line;
#if ($line =~ /altos_veclist\(.*?\)/) {
if ($line =~ /altos_veclist/) {
# 計算 " 包住的字串總數
my $count = () = $line =~ /".*?"/g;
# 在其下一行添加結果
push @updated_lines, "// 字串總數: $count\n";
}
}
# 寫入處理後的內容到新檔案
open my $out_fh, '>', $output_file or die "無法創建檔案 $output_file: $!";
print $out_fh @updated_lines;
close $out_fh;
print "已處理完成,結果存入 $output_file\n";
2024年10月20日 星期日
Regular Expression, regex
來源: https://tw.alphacamp.co/blog/regex
Regex的基本元素
1. 字元匹配
.:匹配任何單個字元(除了換行符\n)。
\d:匹配任何數字,等同於[0-9]。
\D:匹配任何非數字字元,等同於[^0-9]。
\w:匹配任何字母數字字元,包括底線,等同於[A-Za-z0-9_]。
\W:匹配任何非字母數字字元。
\s:匹配任何空白字元(包括空格、製表符、換行符等)。
\S:匹配任何非空白字元。
2. 量詞
*:匹配前面的字元0次或多次。
+:匹配前面的字元1次或多次。
?:匹配前面的字元0次或1次。
{n}:匹配前面的字元恰好n次。
{n,}:匹配前面的字元至少n次。
{n,m}:匹配前面的字元至少n次,但不超過m次。
3. 位置匹配
^:匹配輸入字串的開始位置。
$:匹配輸入字串的結束位置。
\b:匹配一個字詞邊界。
\B:匹配非字詞邊界。
4. 字元集
[abc]:匹配任何一個列在方括號中的字元(此例中為”a”、”b”或”c”)。
[^abc]:匹配任何不在方括號中的字元。
5. 分組和引用
(abc):匹配並捕獲括號內的表達式(此例中為”abc”)。
(?:abc):匹配括號內的表達式但不捕獲匹配的字串。
\1:引用第一個捕獲組的匹配內容。
6. 或運算
|:匹配左側或右側的表達式。
Regex的應用範例
範例1:驗證電子郵件地址
假設我們需要確認一個字符串是否為有效的電子郵件地址,我們可以使用以下正規表達式:
regexCopy code
^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$
^ 表示匹配開始。
\w+ 匹配一個或多個字母、數字或底線。
([\.-]?\w+)* 匹配0個或多個由點或連字符後跟一個或多個字母、數字或底線組成的序列。
@ 是字面量字符,匹配”@”。
\w+([\.-]?\w+)* 匹配域名部分。
(\.\w{2,3})+$ 匹配一個點後跟2到3個字母的頂級域名。
範例2:提取日期
如果我們有一串文本,需要從中提取出所有符合特定格式的日期(例如,YYYY-MM-DD),我們可以使用以下正規表達式:
regexCopy code
\b\d{4}-\d{2}-\d{2}\b
\b 表示單詞邊界。
\d{4} 匹配4位數字(年份)。
- 是字面量字符,匹配”-“。
\d{2} 匹配2位數字(月份和日期)。
範例3:密碼強度檢查
為了確保用戶設定的密碼至少包含8個字符,且包含大小寫字母、數字和特殊字符,我們可以使用以下正規表達式:
regexCopy code
^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}$
^ 和 $ 分別匹配字符串的開始和結束。
(?=.*[a-z]) 確保字符串中至少有一個小寫字母。
(?=.*[A-Z]) 確保至少有一個大寫字母。
(?=.*\d) 確保至少有一個數字。
(?=.*[@$!%*?&]) 確保至少有一個特殊字符。
透過這些範例,我們可以看到正規表達式如何在不同場景下提供強大的文本匹配和處理能力。掌握正規表達式不僅能夠幫助你更有效地處理文本數據,還能提升你的程式設計和數據分析技能。隨著練習和應用的深入,你將能夠解鎖正規表達式更多的潛力,將其應用到更廣泛的領域中。
dotAll Flag, /s
ES 2019 新增 /s 的標籤,過去 . 可以用來匹配除了換行符號以外(\n, \r)的所有字元:
// 過去 . 可以匹配到除了「換行符號」以外的所有字元
console.log(/./.test('\n')); // → false
console.log(/./.test('\r')); // → false
過去雖然可以使用 [\w\W] 來匹配到換行符號,但這不是最好的做法:
console.log(/[\w\W]/.test('\n')); // → true
console.log(/[\w\W]/.test('\r')); // → true
在 ES 2019 中,只要最後有標記 /s 的標籤,如此 . 將也能夠匹配到換行符號:
console.log(/./s.test('\n')); // → true
console.log(/./s.test('\r')); // → true
2024年10月17日 星期四
Keeping skeleton: remove values in LIB
use strict;
use warnings;
#輸入檔案和輸出檔案
my $input_file = $ARGV[0];
my $output_file = $input_file.". skeleton";
# 讀取輸入檔案的內容
open my $in, '<', $input_file or die "can't open file $input_file: $!";
my $content = do { local $/; <$in> };
close $in;
#使用正規表示式處理跨行的 { ... } 區塊
#$content = s/values\s*\(.*?\); \s*//gs;
#移除 vector (ccsp_template2) { }
$contents/vector \(ccsp_template2\) {.*?)\s*//gs;
#移除 (delay_template_8x8) ( )
$content = s/\(delay_template_8x8\) (.*?)\s*//gs;
#移除 (power_template_8x8) ( )
$content =- s/\(power_template_8x8\) (.*?)\s*//gs;
#將結果寫入輸出檔案
open my $out, >', $output_file or die "Can't write contents into file $output_file: $!";
print $out $content;
close $out;
print "Output data to new file $output_file\n";
訂閱:
文章 (Atom)
標籤
- Perl (28)
- TCL (10)
- UNIX Linux (44)
- Uncategoried (1)
- 大盤 (6)
- 書刊雜誌 (11)
- 未分類資料夾 (2)
- 英文 (28)