Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

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

Others pointed out the mistakes with the match. I shall talk further improvements. Here's how I would do it:

  • I always use strict and use diagnostics so that Perl points out any dumb mistakes I make.
  • Instead of traditional filehandles (CINGULAR) I use a scalar variable ($fh). It does not matter in this small program, but in larger I can take advantage of limiting the scope of the variable so that it doesn't unnecessarily pollute the namespace.
  • I tell open to open the file for reading only with the <. Leaving it off means open for read/write, which can be dangerous if overlooked.
  • The idiom is open or die instead of the variant with the logical operator || which requires lots of parentheses because it has such a high operator precedence.
  • This is probably the most important hint: reading the whole file into an array to process it does not scale well. Once your file gets bigger than your RAM, the operating system must page out to disk and the program will become dog slow. A better approach is to read it line for line and process immediately.
use strict; use diagnostics; open my $fh, "<c:/documents and Settings/david price/my documents/cing +ular.txt" or die "cannot open for reading: $!"; my $matches; while (<$fh>) { $matches++ if /9432$/; last if $. == 100; # skip to end of while once we have reached line 100 # $. is a built-in variable, see perlvar in the docs }; close $fh; print "Found $matches matches.\n";

In reply to Re: help manipulating data in an array. by Anonymous Monk
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 pondering the Monastery: (5)
As of 2024-04-25 09:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found