Noah wrote:
Hi there fellow PERL coders.

Hello,

I am trying to match lines between a template file and a configuration file. If the configuration is missing a particular line that is found in the template file then it is printed. If the configuration file has an extra line then it to gets printed.

Something isnt working here. I am not getting the lines to match an I cant seem to figure out why. I am hoping somebody can shed some light here.


#!/sw/bin/perl -w
#
#

my $version_directory = "VERSION";
my $config_directory = "CONFIGS";
my $log_directory = "LOGS";
my $nologin_directory = "NOLOGIN";
my $template_directory = "Configuration_Templates";
my @config_template_filenames = ("$template_directory/blah1.sample.configuration.txt","$template_directory/blah2.sample.configuration.txt");
my $filename_match = ".set.config.txt";

open (blah1, $config_template_filenames[0]) || die "Cannot open '$config_template_filenames[0]' $!";
@blah1_file_lines=<blah1>;
close (blah1);

open (blah2, $config_template_filenames[1]) || die "Cannot open '$config_template_filenames[1]' $!";
@blah2_file_lines=<blah2>;



close (blah2);

for (@blah2_file_lines) {
    print $_;
}

# verify the directories are there
if (!(-d $version_directory)) {
    mkdir $version_directory;
}
if (!(-d $log_directory)) {
    mkdir $log_directory;
}
if (!(-d $config_directory)) {
    mkdir $config_directory;
}
if (!(-d $nologin_directory)) {
    mkdir $nologin_directory;
}

foreach $file (<$config_directory/*.set.config.txt>) {
    open (INPUT,$file)  || die "Cannot open '$file' $!";
    @config_file_lines = <INPUT>;
    close (INPUT);
    undef (@template_file_lines);
    for $line (@config_file_lines) {
    if ($line =~ /LSQ_blah1_OPTIONS/) {
              @template_file_lines = @blah1_file_lines;
        $template_type = "blah1";
        } elsif ($line =~ /blah2-T1-OPTIONS/) {
@template_file_lines = @blah2_file_lines; $template_type = "blah2";
    }
    }

die "Cant figure out where to use blah1 or blah2 template configuration file.\n\n\n" if (!(@template_file_lines));

    $hash1{$_} = 1 foreach (@config_file_lines);
    $hash2{$_} = 1 foreach (@template_file_lines);

    %hash1temp = %hash1;
    %hash2temp = %hash2;

    foreach (keys %hash1) {
    delete $hash2temp{$_};
    }

    foreach (keys %hash2) {
    delete $hash1temp{$_};
    }

    print "Extra items in list1:\n";
    print "$_" foreach (sort (keys %hash1temp));

    exit;

    print "Extra items in list2:\n";
    print "$_\n" foreach (keys %hash2temp);
    exit;
}

UNTESTED but you could try it like this:

#!/sw/bin/perl
use warnings;
use strict;

my $version_directory  = 'VERSION';
my $config_directory   = 'CONFIGS';
my $log_directory      = 'LOGS';
my $nologin_directory  = 'NOLOGIN';
my $template_directory = 'Configuration_Templates';
my $blah1 = "$template_directory/blah1.sample.configuration.txt"; my $blah2 = "$template_directory/blah2.sample.configuration.txt";
my $filename_match     = '.set.config.txt';

my $blah1_file_lines = [ do {
    open my $BLAH, '<', $blah1 or die "Cannot open '$blah1' $!";
    <$BLAH>;
    } ];

my $blah2_file_lines = [ do {
    open my $BLAH, '<', $blah2 or die "Cannot open '$blah2' $!";
    <$BLAH>;
    } ];

print @$blah2_file_lines;

# verify the directories are there
-d $version_directory or mkdir $version_directory or die "Cannot mkdir '$version_directory' $!"; -d $log_directory or mkdir $log_directory or die "Cannot mkdir '$log_directory' $!"; -d $config_directory or mkdir $config_directory or die "Cannot mkdir '$config_directory' $!"; -d $nologin_directory or mkdir $nologin_directory or die "Cannot mkdir '$nologin_directory' $!";

for my $file ( <$config_directory/*$filename_match> ) {
    my @config_file_lines = do {
        open my $INPUT, $file or die "Cannot open '$file' $!";
        <$INPUT>;
        };

    my $template_file_lines;
    for ( @config_file_lines ) {
        if ( /LSQ_blah1_OPTIONS/ ) {
            $template_file_lines = $blah1_file_lines;
            last;
            }
        elsif ( /blah2-T1-OPTIONS/ ) {
            $template_file_lines = $blah2_file_lines;
            last;
            }
        }

@template_file_lines or die "Cant figure out where to use blah1 or blah2 template configuration file.\n\n\n";

    print "Extra items in list1:\n";
    print do {
        my %hash;
        @hash{ @$template_file_lines } = ();
        grep !exists $hash2{ $_ }, @config_file_lines;
        };

    print "Extra items in list2:\n";
    print do {
        my %hash;
        @hash{ @config_file_lines } = ();
        grep !exists $hash1{ $_ }, @$template_file_lines;
        };
    }

__END__



John
--
Perl isn't a toolbox, but a small machine shop where you
can special-order certain sorts of tools at low cost and
in short order.                            -- Larry Wall

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


Reply via email to