I've been racking my brains over this for the past 2 days and I cannot figure out what is the problem with my code.
I'd really appreciate it if someone could help me. Let me explain the problem before I paste my code here:
I have written an SNMP trap handler which should write the traps received into 2 different flat files, based on the source of the trap. I have this information in a config file that it reads from. Once I've read it I store it in <ip>,<server> format in an array. When I actually receive the trap, I loop through the array and find the IP using reg exp. Now, herein lies the problem: it doesn't match! I've printed both separately, and they are exactly same. So why wouldn't they match? Below is the code:
I'd be thankful if you could help me find where is the problem :)my $configFile = "servers.conf"; open (DAT, $configFile); my @fileContents = <DAT>; close DAT; my $str; my @allServers; my @bigArray; foreach $str (@fileContents) { if ($str =~ /^\w/) { push (@allServers, $str); } } my $size = scalar(@allServers); my %hashArray; foreach $str (@allServers) { my ($server, $ip) = split ("=", $str); $ip =~ s/^\s+//; $ip =~ s/\s+$//; push (@bigArray, $ip . "," . $server); $hashArray{$ip} = $server; } my ($key, $value, $idx); foreach $str (@bigArray) { print $str . "\n"; # This prints all values correctly } #while (($key, $value) = each (%hashArray)) { # print LOG "$key-$value\n"; #} # above while statement too prints all correctly my $var; # Read the trap info... my $trapInfo = TrapInfo->new(); # simply creates a new object with hos +tname, ip address and var binds print LOG "TrapInfo ipAddr:" . $trapInfo->ipAddress . "--\n"; $var = $trapInfo->ipAddress; ## Please note that following part of code (foreach, if ## exists, while) doesn't work ## It doesn't give any error; simply says no match foreach $str (@bigArray) { print LOG "$str\n"; if ($str eq $var) { print "IP address matched. Proceed to log trap!\n"; last; } else { print "Match not found.\n"; } } print "After for\n"; if (exists ($hashArray{$trapInfo->ipAddress})) { print $trapInfo->ipAddress . " exists!\n"; } while (($key, $value) = each (%hashArray)) { print "KEY: $key--\n"; print "TrapInfo ipAddr: " . $trapInfo->ipAddress . "--\n"; if ($key eq $trapInfo->ipAddress) { print "IP Matches!\n"; } }
In reply to Regular expression match in code by truptivk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |