On 6/23/07, Vahid Moghaddasi <[EMAIL PROTECTED]> wrote:

I am trying to read a colon delimited text file (filter.in) then
search for each field in another file (/etc/passwd) and if it is found
then write that line in the third file (passwd.out).

use File::Copy;

Are you actually using File::Copy? I didn't find any call to it in
your posted code.

use strict;
use warnings;

That's good....

$|=1;           # flush output buffer
open (FILTERfh, "< filter.in") || die "Can not open filter.in: $!\n";
open PASSWDfh, '</etc/passwd' or die "Can not open the file: $!\n";
open PASSWDFILfh, ">passwd.out";

I can't say that I like the style of having filehandle names ending in
"fh", but it's one way to do it. But please standardize the way you
open files; I'd adopt a style most like the second one. By the way,
the "output buffer" that your comment refers to is the buffer for
STDOUT. Is somebody waiting for the output on STDOUT? You didn't
mention that in the task description.

while (<FILTERfh>) {
chomp;
my @input = split /:/, $_;
    for (my $user = 1; $user <= $#input ; $user++) {

Although you may use the three-part for loop to do this, you'll be
more likely to get it correct if you use a range instead:

   for my $user (0..$#input) {  # Not 1..$#input, is it?

And, unless you needed an index, you'll be even more likely to get it
correct if you use a foreach directly on the array:

   for my $user (@input) {  # Now $user is the user, not the index

        print "$input[$user] is being added.\n";
        while (<PASSWDfh>) {

Now you're reading one file in a loop, inside the loop on FILTERfh. Do
you mean to re-read the password file for every line in the outer
loop's file? That sounds slow, but you could do it. (You'll either
need to reopen the file, or use seek() to get back to the start.)

A better algorithm would read through the entire FILTERfh datastream,
storing away what you'll need. Later, when you read the password file
in a single pass, you can compare the data in memory to the data read
from the file.

Does that get you closer to a solution? Good luck with it!

--Tom Phoenix
Stonehenge Perl Training

--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to