viewing paste cl_checkid | Perl

Posted on the | Last edited on
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
#!/usr/bin/perl
use strict;
print "Hello!\n";
 
Main();
 
sub Main {
    my @aFilename = ();
    if(scalar(@ARGV) < 1){
        print "Please enter filenames\n";
        my $sFilein = <>; chop($sFilein);
        @aFilename = split(' ',$sFilein);
    }
    else { @aFilename = @ARGV; }
    print "Filenames given = @aFilename \n";
    Checker(\@aFilename);
    print "End Checker\n";
}
 
sub Checker { my $raFilename = shift;
    my @aFilename = @$raFilename;
    my @aAllFileID;
    foreach(@aFilename){
        my $raFileID = GetID($_,0);
        push(@aAllFileID,$raFileID);
#       PrintFileID($raFileID,$_);
    }
    my @aDataFilename = ("item_db.txt","item_db2.txt");
    my @aDataID;
    foreach(@aDataFilename){
        my $raFileID = GetID($_,1);
        push(@aDataID,@$raFileID);
#       PrintFileID($raFileID,$_);
    }
#   PrintFileID(\@aDataID,"Data");
    CompareID(\@aDataID,\@aAllFileID,\@aFilename);
}
 
sub CompareID{ my ($raDataID,$raAllFileID,$raFilename) = @_;
    my $sI=0;
    my @aFilename = @$raFilename;
    foreach my $sCurfile (@aFilename){
        print "Missing ID for $sCurfile \n:";
        my $raFileID = $$raAllFileID[$sI];
        my @aDataID = @$raDataID;
        my @aFileID = @$raFileID;
        my @aMissindID;
        foreach my $sCurID (@aDataID){
        my $sFound = 0;
#       print "Searching $sCurID : ";
        foreach(@aFileID){
            if($sCurID == $_) { $sFound=1; last; }
        }
        if($sFound==0) {
            push(@aMissindID,$sCurID);
#           print " fail\n";
        }
#       else {
#           print " foud\n";
#       }
        }
        PrintFileID(\@aMissindID,$sCurfile);
        print "\n\n";
        $sI++;
    }
}
 
sub GetID { my ($sFile,$sIsdata) = @_;
    my @aFileID;
    unless(-r "$sFile"){ print "Can't read/found $sFile\n"; next; }
    open FILEHANDLE,"$sFile" or next;
    my $sRegex;
    if($sIsdata) { $sRegex = '\d+' }
    else { $sRegex = '\d+#' }
    foreach(<FILEHANDLE>){
        if($_ =~ /^\/\//) {next; } #ignore comment
        chomp($_);
        if($_ =~ /$sRegex/){
            if($sIsdata) {
                my @aField = split(',',$_); #csv values
                push(@aFileID,$aField[0]); #1st value
            }
            else {
                my $sId = $_;
                $sId  =~ s/#//g;
                push(@aFileID,$sId);
            }
        }
    }
#   PrintFileID(\@aFileID,$sFile);
    return \@aFileID;
}
 
sub PrintFileID { my($raFileID,$sFilename) = @_;
    print "ID for $sFilename:\n";
    foreach(@$raFileID){
        print "$_\n";
    }
}
Viewed 1067 times, submitted by lighta.