Try this:
open (CINGULAR, "c:/documents and Settings/david price/my documents/ci
+ngular.txt") || die "cannot open: $!";
@number=<CINGULAR>;
close (CINGULAR);
# Remove the following commented lines
# $count=0;
# while ($count<100) {
# if (@number=~/^9432$/g) {
# print "match found"; }
# else {
# print "match failed";
# }
# $count++;
# }
# and change them to:
foreach $line ( @number ) {
if ( $line =~ /^9432$/g ) {
print "Match found\n";
} else {
print "Match failed\n";
}
}
The foreach loop allows you to loop through each element of the array, pull out one element at a time ($line), check it for the pattern, and print the appropriate message. You could add a counter in one or both sections of the conditional block to determine the number of matches/non-matches you get and print this as a summary at the end of the script. ( Beats manually counting up the "Match Found" and "Match Failed" lines in the DOS window. :) )
You're going to want to rethink your regular expression, though. :) Think about what the ^ and $ anchors are do. The way you have it written right now, you will only get a match when the number 9432 is the only text in the line.
HTH,
/Larry
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|