腳本正在從輸入目錄中讀取檔案,因為我們有 5 個不同的檔案。我正在嘗試在處理檔案時設定檔案的優先級。
opendir ( INPUT_DIR, $ENV{INPUT_DIR} ) || die "Error in opening dir $ENV{INPUT_DIR}";
my @input_files = grep {!/^\./} readdir(INPUT_DIR);
foreach my $input_file (@input_files)
{
if($input_file =~ m/^$proc_mask}$/i)
{
# processing files
}
}
就像我有 5 個檔案
Creation.txt
Creation_extra.txt
Modify.txt
Modify_add.txt
Delete.txt
現在,一旦我們讀取了這些輸入檔案,我想設定優先處理第一個 Creation_extra.txt 檔案,然后處理 Delete.txt。
我無法設定檔案讀取的優先級然后處理它
uj5u.com熱心網友回復:
如果我理解正確,您希望能夠指出一些應在其他檔案之前處理的高優先級檔案名。這里有一個方法:
use strict;
use warnings;
use feature 'say';
my @files = <DATA>; # simulate reading dir
chomp @files; # remove newlines
my %prio;
@prio{ @files } = (0) x @files; # set default prio = 0
my @high_prio = qw(Creation_extra.txt Delete.txt); # high prio list
# to set high prio we only want existing files
for (@high_prio) {
if (exists $prio{$_}) { # check if file name exists
$prio{$_} = 1; # set prio
}
}
# now process files by sorting by prio, or alphabetical if same prio
for (sort { $prio{$b} <=> $prio{$a} || $a cmp $b } @files) {
say;
}
__DATA__
Creation.txt
Creation_extra.txt
Modify.txt
Modify_add.txt
Delete.txt
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516896.html
標籤:perl
上一篇:在Perl中回圈哈希會產生奇怪的結果-提供的示例腳本
下一篇:如何洗掉陣列中的重復參考?