Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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


In reply to Re: help manipulating data in an array. by larryp
in thread help manipulating data in an array. by nightfreightpilot

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



  • 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.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-04-24 07:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found