in reply to Comparing files with same name in different directories
By does not work I presume you mean that your line counter increments ever upward. You just need to zero it after each file. This will work
#!/usr/bin/perl -w use strict; my $prefixDir = "$ENV{HOME}/sa/current"; my @clusterDirs= ("$prefixDir/cluster1", "$prefixDir/cluster2", "$pref +ixDir/cluster3", "$prefixDir/cluster4", "$prefixDir/cluster5"); use vars qw ($configDir $configFile $lineCount $configFile $fileToComp +are @configFileList); foreach $configDir (@clusterDirs) { opendir DIR, "$configDir" or die "Cannot access $configDir: $!\n"; @configFileList = grep /[^.]/, readdir DIR;print "$configDir\n"; foreach $fileToCompare (@configFileList) { $lineCount = 0; #zero our line count for each file open FILE, "$configDir/$fileToCompare" or die "file not available: $ +!\n"; $lineCount++ while <FILE>; print "file: $fileToCompare: $lineCount\n"; } }
I would recommend using my over use vars/our as my lexically scopes variables whereas vars and our generate globals. MJD has a great tute at http://perl.plover.com/FAQs/Namespaces.html
Here is a quick recode using my. No globals are required. I've also snipped a bit of redundant code.
my $prefixDir = "$ENV{HOME}/sa/current"; my @clusterDirs= qw(cluster1 cluster2 cluster3 cluster4 cluster5); foreach my $configDir (@clusterDirs) { opendir DIR, "$prefixDir/$configDir" or die "Cannot access $prefix +Dir/$configDir: $!\n"; my @configFileList = grep /[^.]/, readdir DIR; print "$configDir\n"; foreach my $fileToCompare (@configFileList) { my $lineCount = 0; open FILE, "$prefixDir/$configDir/$fileToCompare" or die "file + not available: $!\n"; $lineCount++ while <FILE>; close FILE; print "file: $fileToCompare: $lineCount\n"; } }
In my opinion once variable names get too long they start to obfuscate the meaning of the code and are also more prone to typos. I try for 7 or less chars in a var name. I got a bit carried away and generated a 2D hash to store the results and then print the results in a formatted HTML table for easy comparison:
#!/usr/bin/perl -w use strict; my $base_dir = "$ENV{HOME}/sa/current"; my @sub_dir = qw(cluster1 cluster2 cluster3 cluster4 cluster5); my (%all_files, %file); foreach my $sub_dir (@sub_dir) { opendir DIR, "$base_dir/$sub_dir" or die "Cannot access $base_dir/ +$sub_dir: $!\n"; my @files = grep /[^.]/, readdir DIR; foreach my $file (@files) { my $line_count = 0; open FILE, "$base_dir/$sub_dir/$file" or die "file not availab +le: $!\n"; $line_count++ while <FILE>; close FILE; $file{$sub_dir}{$file} = $line_count; $all_files{$file}++; } } # print out a nice HTML table print "<table border='2'>\n"; print "<tr>\n <td>File name</td>\n"; print " <td>$_</td>\n" for @sub_dir; print "</tr>\n"; for my $file (keys %all_files) { print "<tr>\n"; print " <td>$file</td>\n"; for my $sub_dir (@sub_dir) { # avoid undefined warnings if file does not exist # in one or more of our subdirs my $file_size = $file{$sub_dir}{$file} || "undef"; print " <td>$file_size</td>\n"; } print "</tr>\n"; } print "</table>\n";
Hope this helps. cheers
tachyon
s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print
|
|---|