Annotation of loncom/localize/localize/checkduplicates.pl, revision 1.2

1.1       bisitz      1: #!/usr/bin/perl
                      2: # The LearningOnline Network with CAPA
1.2     ! bisitz      3: # $Id: checkduplicates.pl,v 1.1 2009/04/07 10:51:53 bisitz Exp $
1.1       bisitz      4: 
                      5: # 07.04.2009 Stefan Bisitz
1.2     ! bisitz      6: # Optimization ideas by Stefan Droeschler
1.1       bisitz      7: 
                      8: use strict;
                      9: use warnings;
                     10: 
                     11: my $man = "
                     12: checkduplicates - Checks if hash keys in translation files occur more than one time. If so, a warning is displayed.
                     13: 
                     14: The found keys and corresponding values need to be changed. Otherwise, there is no gurantee which value is taken. This is dangerous, if same keys but different values are used or if one value is changed but the screen still shows the old value which actually comes from the other occurence.
                     15: 
                     16: 
                     17: SYNOPSIS:\tcheckduplicates -h 
                     18: \t\tcheckduplicates FILE
                     19: 
                     20: OPTIONS:
                     21: -h\t\tDisplay this help and exit.
                     22: 
                     23: ";
                     24: 
                     25: my $filename; 
                     26: die "Use option -h for help.\n" unless exists $ARGV[0];
                     27: #analyze options
                     28: if ( $ARGV[0] =~ m/^\s*-h/ ) {
                     29: 	print $man;
                     30: 	exit();
                     31: }else{
                     32: 	$filename = ($ARGV[0]);
                     33: 	die "$filename is not a file.\n" unless -f $ARGV[0];
                     34: }
                     35: 
                     36: 
                     37: # ----------------------------------------------------------------
                     38: # Start Analysis
                     39: print "checkduplicates is searching for duplicates in $filename...\n";
                     40: 
                     41: # Manually read all stored keys from translation file (inlcuding probable duplicates)
1.2     ! bisitz     42: # and count key occurrences in a separate hash.
        !            43: my %counter;
1.1       bisitz     44: my $line;
                     45: open( FH, "<", $filename ) or die "$filename cannot be opened\n";
                     46: while ( !eof(FH) ) {
                     47:     $line = readline(FH);
1.2     ! bisitz     48:     next if $line=~/^\s*#/; # ignore comments
1.1       bisitz     49:     #$exprNP=~s/^["'](.*)["']$/$1/; # Remove " and ' at beginning and end
1.2     ! bisitz     50:     if ($line =~ m/^\s+["'](.*)["']/) { # Find "..." or '...' key
        !            51:         $counter{$1}++;
1.1       bisitz     52:     }
                     53: }
                     54: close(FH);
                     55: 
1.2     ! bisitz     56: # Print all keys which occures more than one time
1.1       bisitz     57: my $dupl = 0; # total counter to count when a key occurred more than one time
1.2     ! bisitz     58: foreach my $count_key (keys %counter) {
        !            59:     my $count_value = $counter{$count_key};
        !            60:     if ($count_value > 1) {
        !            61:         print 'Found '.$count_value.' times key: '.$count_key."\n";
        !            62:         $dupl++;
1.1       bisitz     63:     }
                     64: }
1.2     ! bisitz     65: 
1.1       bisitz     66: if ($dupl == 0) {
                     67:     print "Be happy - No duplicates found.\n";
                     68: } else {
                     69:     print "--- Found $dupl duplicate(s) in $filename which need to be corrected!\n";
                     70: }
                     71: 
                     72: # ----------------------------------------------------------------
                     73: 

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>