probably one of the best things you could do to your code is to change your database read/processing. right now, you have two steps:
1) load the database into memory (an array)
2) process each line and print something
reading the data into an array takes unnecessary time and memory in your example. instead, open the file, and process using a while loop, like while(<DATABASE>) ....

that being said, there are a few other things you should do. here are some of them:

> use strict! it will save you many, many, problems. and use warnings, too. either by -w on the shebang line, or, if you have perl >= 5.6.0, use warnings;.

> when you open and close files, check that they were successful, and die (or warn, croak, etc.) if otherwise.

> don't use $a and $b as variable names, as these are generally reserved for sort.

> indent properly. your code is missing a closing brace, and is quite difficult to read without proper indentation.

> did you include enough code? what are $s, $r, $m, $p? i'm not sure why you're using the g modifier on your matches.

> if you're not using $rec later on, you don't need it. you can use $_ instead. i wasn't sure, so i left it in your code in my sample below.

here's a list of notes you might want to read:
while or foreach?
Opening files
Use strict warnings and diagnostics or die
perlre

here's an example of the kind of code i'm talking about. i haven't tested it, so i can't say it works. i've removed

#!/usr/local/bin/perl -w use strict; $|++; use FileHandle; my $path = "/path/to"; my $database = "database"; my $DATABASE = new FileHandle; ## what are these? what are they initialized to? my ($m, $p, $r, $s, $p); open ($DATABASE,"< ", $path/$database") or die("ERROR: cannot open database $path/$database! $!"); while(<DATABASE>) { my $rec = $_; chomp($rec); ## not good variable names, be descriptive my ($a, $b, $c, $d, $e, $f) = split(/\|/, $rec); if ($s =~ /$d/g) { if ($r =~ /$e/g){ if ($m =~ /$fa/g){ print "$b\n"; if ($c eq "Y"){ print "$a - tal\n"; } if ($p eq "Y"){ print "$e"; } } } ## ... do more stuff } ## ... do more stuff } close($DATABASE) or die("ERROR: cannot close database $path/$database! $!");

~Particle


In reply to Re: Faster Flat File by particle
in thread Faster Flat File by Buzz

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.