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?
I am not exactly sure what your problem is Sakura, but I think it may be that you aren’t finding matches between the
$configand$base_configwhen you should. I suspect this may be because of leading/trailing whitespace, and suggest you writeYou also need to make sure that
$base_confighas the same treatment before you compare its values.