Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
The simplest way to do this is as follows:
use strict; use warnings; my (@numbers, $handle); open ($handle, "c:/documents and Settings/david price/my documents/cin +gular.txt") || die "cannot open: $!"; @numbers = <$handle>; close ($handle); for (@numbers) { if (index($_, '9432') != -1) { print "match found\n"; } else { print "match failed\n"; } }
Use strict and use warnings will tell you if you do naughty things with your code, like forgetting to set scope on your variables (my means scope is inside the current block). Using (locally scoped) variables for your handles rather than names means your handle can't conflict with other handles; using a for loop on your lines means you don't have to mess with line numbers; using substr means your matching is much more efficient than if you used regular expressions.

Line by line analysis of your code is as follows:

open (CINGULAR, "c:/documents and Settings/david price/my documents/ci +ngular.txt") || die "cannot open: $!"; # USE VARIABLES FOR FILE HANDLES INSTEAD OF NAMED FILE HANDLES @number=<CINGULAR>; # DECLARE A SCOPE FOR ALL VARIABLES. my @number=<CINGULAR>; would be b +etter close (CINGULAR); $count=0; # SAME while ($count<100) { # IF YOU WANT TO LOOP THROUGH AN ARRAY, USE SOMETHING LIKE: # for (my $c = 0; $c <= $#number; $c++) { print $number[$c]; } # for (0..$#number) { print $number[$_]; } # for (@number) { print $_; } # foreach (@number) { print $_; } # $#number is the address of the last item in the array. # $_ is the default input / output variable. if (@number=~[/^9432$/g]) { # PROPER SYNTAX IS if ($number[$count] =~ /9432$/) { # The g flag is used for multiple matches; ^ and $ are only used toget +her # if you want a match at both ends - in other words, an exact match. # I don't know what the brackets were for. print "match found"; } else { print "match failed"; } $count++; # THIS IS FINE. HOWEVER, YOU COULD HAVE SAVED A STATEMENT BY DOING: # while ($count++ < 100) { }

In reply to Re: help manipulating data in an array. by TedPride
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 lurking in the Monastery: (4)
As of 2024-04-20 09:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found