如何按文件名多组件排序文件列表?Perl代码未达预期求修正
修正Perl代码实现按指定规则排序文件列表
需要对文件列表按文件名的多组件规则排序,核心排序依据为Color MODE(wc、gs、pc),同时结合文件类型(f/bio)和序号。输入输出示例如下:
| INPUT | OUTPUT |
|---|---|
| PDIS_01220157RE_pc_bio1.tif | PDIS_01220157RE_gs_f1.tif |
| PDIS_01220157RE_pc_bio2.tif | PDIS_01220157RE_wc_f1.tif |
| PDIS_01220157RE_pc_bio3.tif | PDIS_01220157RE_gs_f2.tif |
| PDIS_01220157RE_pc_bio4.tif | PDIS_01220157RE_wc_f2.tif |
| PDIS_01220157RE_wc_bio1.tif | PDIS_01220157RE_gs_f3.tif |
| PDIS_01220157RE_wc_bio2.tif | PDIS_01220157RE_wc_f3.tif |
| PDIS_01220157RE_wc_bio3.tif | PDIS_01220157RE_gs_f4.tif |
| PDIS_01220157RE_wc_bio4.tif | PDIS_01220157RE_wc_f4.tif |
| PDIS_01220157RE_gs_f1.tif | PDIS_01220157RE_gs_f5.tif |
| PDIS_01220157RE_wc_f1.tif | PDIS_01220157RE_wc_f5.tif |
| PDIS_01220157RE_gs_f2.tif | PDIS_01220157RE_gs_f6.tif |
| PDIS_01220157RE_wc_f2.tif | PDIS_01220157RE_gs_f7.tif |
| PDIS_01220157RE_gs_f3.tif | PDIS_01220157RE_gs_f8.tif |
| PDIS_01220157RE_wc_f3.tif | PDIS_01220157RE_gs_f9.tif |
| PDIS_01220157RE_gs_f4.tif | PDIS_01220157RE_wc_f9.tif |
| PDIS_01220157RE_wc_f4.tif | PDIS_01220157RE_pc_f10.tif |
| PDIS_01220157RE_gs_f5.tif | PDIS_01220157RE_wc_f10.tif |
| PDIS_01220157RE_wc_f5.tif | PDIS_01220157RE_wc_f11.tif |
| PDIS_01220157RE_gs_f6.tif | PDIS_01220157RE_wc_f12-1.tif |
| PDIS_01220157RE_gs_f7.tif | PDIS_01220157RE_pc_f12-1.tif |
| PDIS_01220157RE_gs_f8.tif | PDIS_01220157RE_wc_f12-2.tif |
| PDIS_01220157RE_gs_f9.tif | PDIS_01220157RE_pc_f12-2.tif |
| PDIS_01220157RE_wc_f9.tif | PDIS_01220157RE_wc_f12-11.tif |
| PDIS_01220157RE_pc_f10.tif | PDIS_01220157RE_gs_f12-11.tif |
| PDIS_01220157RE_wc_f10.tif | PDIS_01220157RE_wc_bio1.tif |
| PDIS_01220157RE_wc_f11.tif | PDIS_01220157RE_pc_bio1.tif |
| PDIS_01220157RE_gs_f12-11.tif | PDIS_01220157RE_wc_bio2.tif |
| PDIS_01220157RE_pc_f12-1.tif | PDIS_01220157RE_pc_bio2.tif |
| PDIS_01220157RE_pc_f12-2.tif | PDIS_01220157RE_wc_bio3.tif |
| PDIS_01220157RE_wc_f12-1.tif | PDIS_01220157RE_pc_bio3.tif |
| PDIS_01220157RE_wc_f12-11.tif | PDIS_01220157RE_wc_bio4.tif |
| PDIS_01220157RE_wc_f12-2.tif | PDIS_01220157RE_pc_bio4.tif |
原代码问题分析
原代码仅针对_f后的数字做排序,完全未处理Color MODE的优先级规则,也未区分f和bio类型的分组,且两个if语句会互相覆盖排序结果,无法匹配预期输出。
修正后的代码
#--------------------> sub IMGSValidations #--------------------> { my @ChkAllImgs = @_; my $figname = join "\n", @ChkAllImgs; # 定义Color MODE优先级:f类遵循gs > wc > pc,bio类遵循wc > pc my %f_color_priority = (gs => 1, wc => 2, pc => 3); my %bio_color_priority = (wc => 1, pc => 2); my @sorted_array = sort { # 解析文件名的核心字段:Color MODE、文件类型、主序号、子序号 my ($a_color, $a_type, $a_main, $a_sub) = $a =~ /_([wgp]c)_(f|bio)(\d+)(?:-(\d+))?\./; my ($b_color, $b_type, $b_main, $b_sub) = $b =~ /_([wgp]c)_(f|bio)(\d+)(?:-(\d+))?\./; # 无连字符的文件名,子序号默认设为0 $a_sub //= 0; $b_sub //= 0; # 第一优先级:文件类型,f类排在bio类之前 my $type_cmp = ($a_type eq 'f' ? 0 : 1) <=> ($b_type eq 'f' ? 0 : 1); return $type_cmp if $type_cmp != 0; # 第二优先级:主序号数字升序 my $main_cmp = $a_main <=> $b_main; return $main_cmp if $main_cmp != 0; # 第三优先级:子序号数字升序(仅f类存在带连字符的情况) my $sub_cmp = $a_sub <=> $b_sub; return $sub_cmp if $sub_cmp != 0; # 第四优先级:Color MODE优先级,按文件类型匹配规则 if ($a_type eq 'f') { return $f_color_priority{$a_color} <=> $f_color_priority{$b_color}; } else { return $bio_color_priority{$a_color} <=> $bio_color_priority{$b_color}; } } @ChkAllImgs; $figname = join "\n", @sorted_array; my $FigListText = "$curDir/Test.txt"; writeFileinString($FigListText, \$figname); }
关键逻辑说明
- 字段解析:通过正则表达式提取每个文件名的Color MODE、文件类型、主序号、子序号(带连字符的情况)
- 排序层级:
- 先按文件类型排序,
f类始终在bio类之前 - 同类型下按主序号数字升序排序
- 主序号相同时,按子序号数字升序排序
- 序号完全相同时,按预设的Color MODE优先级排序
- 先按文件类型排序,
- 默认值处理:对无连字符的文件名设置默认子序号为0,确保排序逻辑统一
内容的提问来源于stack exchange,提问作者ssr1012
相关产品推荐
相关产品推荐

