in reply to Re^2: Regular expression match in code
in thread Regular expression match in code

my $var = "62.40.40.30"; print "var: $var\n"; ## 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 "$str\n"; if ($str =~ /^$var/) { print "IP address matched. Proceed to log trap!\n"; last; } else { print "Match not found.\n"; } }

You iterate over all values in the array. Since one of the items in the array matches the regex, you get one line of output saying IP address matched. Proceed to log trap, and for all items before the match you get the output that no match was found.

If you don't want that output even though you can match later on, then don't print it. Only print it if the iteration is finished, and you didn't find any matches.

if (exists ($hashArray{$var})) { print $var . " exists in hash!\n"; } while (($key, $value) = each (%hashArray)) { print "KEY: $key\n"; if ($key eq $var) { print "IP Matches!\n"; } }

A hash is a better approach to what you try to do, but you do it overly complicated. Instead of iterating over it, you can get the value straigt out of the hash:

if (exists ($hashArray{$var})){ print "$var exists in the hash.\n"; print "Its value is $hashArray{$var}\n"; }

Whenever you iterate over a hash to see if the key is in there, you're doing something wrong.

So with all three methods you actually find the IP you are looking for (and using the hash access is the easiest and most straight forward).

Perl 6 - links to (nearly) everything that is Perl 6.

Replies are listed 'Best First'.
Re^4: Regular expression match in code
by truptivk (Novice) on May 12, 2010 at 09:06 UTC
    The code I gave in my last reply with hard-coded values for IP addresses, works. Even adding your snippet for "if exists" works. But when I use it in my original code, which reads values from config file, then it doesn't work. Yes, even the snippet you suggested doesn't work. I'm really at my wits end here... :(
      Try to inspect your data with
      use Data::Dumper; $Data::Dumper::Useqq = 1; print Dumper $var; print Dumper \@allServers;

      You might have some non-printable characters either in the server list or in $var that prevent you from getting matches.

        Surprise surprise,
        print Dumper \@allServers; print Dumper \@bigArray;
        prints nothing; just  $VAR1 = []  print Dumper $var; correctly prints the IP present in the incoming SNMP trap. So my reading from file part is going wrong?! How? Its fairly straight forward and I have even tested it by writing  exit(0); after the read from config (populates @allServers) and write into @bigArray (<ip,server>) format. Help... :(
      The code I gave in my last reply with hard-coded values for IP addresses, works.

      truptivk: what follows is intended not as a rebuke, but for future reference. When moritz asked for example code to run, the implicit assumption was that the code would demonstrate the problem, not that it would run just fine. I may not have been the only monk who spent time trying to figure out what (nothing) was wrong with the code. (Well, maybe just a little bit of a rebuke:)

        I understand, and I'm extremely sorry for taking some/many monks' time. I should've found a way to provide code that demonstrated my problem, rather than hard-code values which made it run correctly. Not to make an excuse, but I'm overwhelmed at work with lots to do, and I did the mistake. I sincerely apologise once again. Meanwhile, I'm still trying to figure out what's wrong with my code.

      It might help if you posted a copy of your log file.