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.

In reply to Re^2: Regular expression match in code by truptivk
in thread 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.