On Wed, Feb 18, 2009 at 15:43, mritorto <mrito...@gmail.com> wrote:
> guys
>
> I am new to perl scripting.
>
> I am trying to read 2 different txt files into 2 different arrays.
>
> My goal is to have a script read them both and print out any
> differences.
>
>
> Here is the script - it only prints out some of the files that end in
> a *.cer extentions but is miss the other info
>
>
> ###### perl script
>
>
>
> #! ./perl
> #compares directories on 2 servers
> open (INPUT, "<atlasdirectorylisting.txt");
> open (INPUT2, "<ISISdirectorylisting.txt");
> open (OUTPUT,">compare.log");
> @lines = <INPUT>;
> @lines2 = <INPUT2>;
>
> %lines = map {$_,1} @lines;
> @difference = grep {!$lines {$_}} @lines2;
> print "@difference\n";
>
>
> print OUTPUT "@difference\n";
>
>
>
> close (INPUT);
> close (INPUT2);
> close (OUTPUT);
snip
> What am i missing?
snip

This is not the best way to go about solving this problem (use diff or
a module like Text::Diff*), but this isn't the most horrible thing in
the world if your files are known to be small.  It is important to
note that this algorithm only works if you care about unique lines.
If you need to know that file1 has 5 lines of "foo" and file2 has 4
lines of "foo", this code will not tell you that.

Your main problem is that you are only looking for lines in the ISIS
file that aren't in the atlas file.  Other issues include:
    1. lack of the use of strict and warnings
    2. issues arising from lack of the strict pragma (undeclared variables)
    3. use of bareword word file handles
    4. use of the two argument version of open
    5. failure to check for errors on opening the files
    6. useless variables names, quick tell me if @lines or @lines2 is
the atlas file?
    7. wasted memory in the form of intermediate values that are used once
    8. the file names are hardcoded instead of passed on the
commandline (I kept this issue because I do not know enough about what
you are doing to correctly generalize it)


#!/usr/bin/perl

use strict;
use warnings;

open my $atlas, "<", "atlasdirectorylisting.txt"
        or die "could not open atlasdirectorylisting.txt: $!";
open my $isis, "<", "ISISdirectorylisting.txt"
        or die "could not open ISISdirectorylisting.txt: $!";

my %atlas = map { $_ => 1 } <$atlas>;
my %isis  = map { $_ => 1 } <$isis>;

print "in atlas but not in isis\n",
        (
                map { "\t$_" }
                grep {!$atlas{$_}} keys %isis
        ),
        "in isis but not is atlas\n",
        (
                map { "\t$_" }
                grep {!$isis{$_}} keys %atlas
        );


* http://search.cpan.org/dist/Text-Diff/lib/Text/Diff.pm

-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to