Larry Wall (the creator of Perl) often says and writes that one of the primary qualities of a programmer is laziness. To tell the truth, laziness sometimes requires some work, but the time gained is often really worth the effort. Your program is a pathological case of lack of laziness.
There are several things you could do to divide your program's length by a factor of 10 or probably even more.
you could replace these 28 individual arrays by an array of arrays (AoA) or a hash of arrays (HoA):my @dev_flash000 = (); my @dev_flash001 = (); # ... my @dev_flash027 = ();
Actually, the initializing loop is not necessary, the structure will auto-vifify when you use it, but I wanted to show the whole thing for pedagogical purpose. So, this code could boil down to just 1 line.my %dev_flash; # an HoA for my $index (0..27) { @{$dev_flash{$index}} = (); }
Now the initial part of your extract_file subroutine:
can be reduced to three lines using an array instead of 28 scalars:my $dev_flash000 = read_file('temp/dev_flash000'); my $dev_flash001 = read_file('temp/dev_flash001'); # ... my $dev_flash001 = read_file('temp/dev_flash027');
Having done that, your huge repetitive if /elsif can be reduced to a dozen lines of code within a loop.my @dev_content; # using a different variable name for clarity for my $i (0..27) { my $file_index = sprintf "%02i", $i; $dev_content[$_] = read_file("temp/dev_flash0$file_index") }
Something like this:
I did not code the details because, on the one hand, I would prefer to leave it to you as an exercise, but mainly because all this would still be very inefficient. Notice that the two last snippets of code are within identical loops, so there is no point to build two loops, one is enough doing the two things. I leave it to you to do it. (But I would gladly try to help you if you encounter difficulties in doing it, and I am sure many other monks will also try to help you.)for my $i (0..27) { my $file_index = sprintf "%02i", $i; # ... } }
But more than that, it seems to me (but I am not sure) that you are doing a lot of useless work: you are calling the read_file subroutine 28 times, but, in effect you are using only 1 of them, and you do that each time you call the extract_file sub. I am not sure I understood everything you are doing, because you code is far too long for someone lazy like me ;-) to read it all in details, but there is probably a way to factor this out.
Even if you don't want to use a HoA and an array the way I showed above and want to keep your data the way you have it, you could still cut down considerably your extract_file sub by calling another sub (within a loop or after each assignment) with the right parameters to do the comparisons.
and store your nested comparisons within the compare sub.my $dev_flash000 = read_file('temp/dev_flash000'); compare($file, $dev_flash000, 'temp/dev_flash000'); my $dev_flash001 = read_file('temp/dev_flash001'); compare($file, $dev_flash001, 'temp/dev_flash001'); # ... my $dev_flash001 = read_file('temp/dev_flash027'); compare($file, $dev_flash027, 'temp/dev_flash027');
Do yourself a favor: be lazy. ;-)
In reply to Re: directories and sub directories and copying or moving hundereds or thousands of files :)
by Laurent_R
in thread directories and sub directories and copying or moving hundereds or thousands of files :)
by james28909
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |