Hello all,

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:

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"; } }
I'd be thankful if you could help me find where is the problem :)

In reply to Regular expression match in code by truptivk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.