in reply to Regular expression match in code

In the title of your node you mention a regular expression, but in the code you write
if ($str eq $var)

Which is not a regex match, but an exact string comparison.

I'd also advise you to use strict; use warnings; in your code, and post code that we can actually run (ie not containing references to config files we don't know about, modules we don't have access to etc.), and reduce it to the part that's actually relevant.

You can use Data::Dumper to inspect your data structures.

Replies are listed 'Best First'.
Re^2: Regular expression match in code
by truptivk (Novice) on May 12, 2010 at 08:40 UTC
    Oops, my bad. I did use regular expression first, because I'd be comparing IP address from trap to values in an array in format <ip address>,<server>. So I had used:
    if ($str =~ /^$var/)
    which didn't work either. I'm already using  use strict; use warnings;. I'm again pasting code that one can run:
    use strict; use warnings; my %hashArray; my $str; my @bigArray; # Adding to an array in format <ip,server> push (@bigArray, "62.40.40.10,ServerOne"); push (@bigArray, "62.40.40.20,ServerTwo"); push (@bigArray, "62.40.40.30,ServerThree"); # Adding to hash $hashArray{"62.40.40.10"} = "ServerOne"; $hashArray{"62.40.40.20"} = "ServerTwo"; $hashArray{"62.40.40.30"} = "ServerThree"; my ($key, $value, $idx); foreach $str (@bigArray) { print $str . "\n"; # This prints all values correctly } while (($key, $value) = each (%hashArray)) { print "$key-$value\n"; } # above while statement too prints all correctly 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"; } } print "After for\n"; 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"; } }
    I hope this helps... Thanks.
      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.
        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... :(