【发布时间】:2011-07-25 02:29:45
【问题描述】:
我必须从a file 读取行并将它们存储到 Perl 中的哈希中。其中许多行在开头都有特殊的字符序列,我需要在存储之前将其删除。这些字符序列是
| || ### ## @@||
比如如果是||https://ads,我需要得到https://ads;如果###http,我需要得到http。
我需要排除这些字符序列。我想通过将所有字符序列排除在一个数组中来做到这一点,然后检查该行是否以这些字符序列开头并删除它们。有什么好的方法可以做到这一点?
我已经做到了:
our $ad_file = "C:/test/list.txt";
our %ads_list_hash = ();
my $lines = 0;
# List of lines to ignore
my @strip_characters = qw /| || ### ## @@||/;
# Create a list of substrings in the easylist.txt file
open my $ADS, '<', $ad_file or die "can't open $ad_file";
while(<$ADS>) {
chomp;
$ads_list_hash{$lines} = $_;
$lines ++;
}
close $ADS;
如果存在任何@strip_characters,我需要添加逻辑以从每行的开头删除它们。
【问题讨论】:
-
将所有字符放在一个表达式中,然后用它来替换(删除)不需要的字符