Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: help manipulating data in an array.

by larryp (Deacon)
on Nov 12, 2004 at 04:48 UTC ( [id://407274]=note: print w/replies, xml ) Need Help??


in reply to help manipulating data in an array.

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

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://407274]
help
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-19 15:26 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found