Perl:哈希和正则表达式中的键问题
很抱歉发布另一个与我之前发布的问题类似的问题。我意识到我的问题不是很清楚,这可能会导致答案中出现一些误解。所以我想重新表述并再次询问。
我的任务是读入 2 个文件(基本配置文件和配置文件)。两个文件都可以有任意行数。行的顺序不必按顺序排列。我需要忽略“!”之后的事情和“^”。然而,我被困在忽略“!”的部分。和“^”。我能够将每一行存储到哈希中的键中(没有“!”或“^”之后的内容),但当我进行比较时它失败了。
例如,如果文件中有一行“hello!123”,我需要在哈希中仅存储“hello”,并将字符串“hello”与另一个哈希中的另一个键进行比较。如果另一个哈希中有一个“hello”键,我需要将其打印出来或将其放入另一个哈希中。我的程序只能将“hello!123”行中的“hello”放入其中,但在与另一个哈希中的另一个键进行比较时,该部分失败了。
我通过编写另一个简短的程序来检查我的正则表达式,该程序只接受用户输入并删除“!”之后的内容。和“^”符号并与另一个哈希的另一个键进行比较。
这是我的错误代码:
my %common=();
my %different=();
#open config file and load them into the config hash
open CONFIG_FILE, "< script/".$CONFIG_FILENAME or die;
my %config;
while (<CONFIG_FILE>) {
chomp $_;
$_ =~ s/(!.+)|(!.*)|(\^.+)|(\^.*)//;
$config{$_}=$_;
print "_: $_\n";
#check if all the strings in BASE_CONFIG_FILE can be found in CONFIG_FILE
$common{$_}=$_ if exists $base_config{$_};#stored the correct matches in %common
$different{$_}=$_ unless exists $base_config{$_};#stored the different lines in %different
}
close(CONFIG_FILE);
有人遇到和我一样的问题吗?你会做什么来解决它?
Sorry to post another question that is similar to the question that I posted earlier. I realised that my question is not very clear and it might have led to some misunderstanding in the answers. So I thought of rephrasing it and ask again.
My task is to read in 2 files (base config file and config file). Both files can have any number of lines. The order of the lines need not be in sequence. I need to ignore things after "!" and "^". However, I am stuck at the part on ignoring the "!" and "^". I am able to store each line into keys in a hash (without the things after "!" or "^") but it fails when I am comparing.
For example, if I have a line "hello!123" in the file, I need to store only "hello" in the hash and compare the string "hello" with another key from another hash. If there is a "hello" key in the other hash, I need to print it out or put it into another hash. My program can only put the "hello" from the line "hello!123" but failed at the part when it is comparing with another key in the other hash.
I have checked my regular expression by writing another short program that just takes in user input and remove the things after "!" and "^" symbols and compare with another key of the other hash.
Here is my faulty code:
my %common=();
my %different=();
#open config file and load them into the config hash
open CONFIG_FILE, "< script/".$CONFIG_FILENAME or die;
my %config;
while (<CONFIG_FILE>) {
chomp $_;
$_ =~ s/(!.+)|(!.*)|(\^.+)|(\^.*)//;
$config{$_}=$_;
print "_: $_\n";
#check if all the strings in BASE_CONFIG_FILE can be found in CONFIG_FILE
$common{$_}=$_ if exists $base_config{$_};#stored the correct matches in %common
$different{$_}=$_ unless exists $base_config{$_};#stored the different lines in %different
}
close(CONFIG_FILE);
Does anyone has the same problem as me before? What do you do to solve it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不太确定 Sakura 的问题是什么,但我认为可能是您在应该找到
$config
和$base_config
之间的匹配项。我怀疑这可能是由于前导/尾随空格造成的,建议您编写您还需要确保在比较其值之前
$base_config
具有相同的处理方式。I am not exactly sure what your problem is Sakura, but I think it may be that you aren't finding matches between the
$config
and$base_config
when you should. I suspect this may be because of leading/trailing whitespace, and suggest you writeYou also need to make sure that
$base_config
has the same treatment before you compare its values.这是我采取的方法 - 请注意代码未经测试,我刚刚醒来:)你可能需要修复一些事情(猜测在分割线附近......)但想法是有效的。
This is the approach I'd take - please note the code is untested and I've only just woken up :) You may have to fix a couple of things (near the split line at a guess...) but the idea is valid.