in reply to directories and sub directories and copying or moving hundereds or thousands of files :)

Hi james28909,

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.

my @dev_flash000 = (); my @dev_flash001 = (); # ... my @dev_flash027 = ();
you could replace these 28 individual arrays by an array of arrays (AoA) or a hash of arrays (HoA):
my %dev_flash; # an HoA for my $index (0..27) { @{$dev_flash{$index}} = (); }
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.

Now the initial part of your extract_file subroutine:

my $dev_flash000 = read_file('temp/dev_flash000'); my $dev_flash001 = read_file('temp/dev_flash001'); # ... my $dev_flash001 = read_file('temp/dev_flash027');
can be reduced to three lines using an array instead of 28 scalars:
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") }
Having done that, your huge repetitive if /elsif can be reduced to a dozen lines of code within a loop.

Something like this:

for my $i (0..27) { my $file_index = sprintf "%02i", $i; # ... } }
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.)

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.

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');
and store your nested comparisons within the compare sub.

Do yourself a favor: be lazy. ;-)

  • Comment on Re: directories and sub directories and copying or moving hundereds or thousands of files :)
  • Select or Download Code

Replies are listed 'Best First'.
Re^2: directories and sub directories and copying or moving hundereds or thousands of files :)
by james28909 (Deacon) on Oct 04, 2014 at 22:38 UTC
    yeah that is def something i am going to have to learn, array of array and hash of arrays. but the reason i did it like that was i needed to make sure the files found in dev_flash folder would be compared to all the filenames in files of the temp folder and logically that is what i worked out in my primitive brain lol. i do see your logic tho and i must learn how to use array of array. that initially was my first guess, but ofcourse i did not know much about it or how to use them so i made that script to write the loops for me and then copy pasta the code lol. and thanks for yoru input, this is exactly what i want (and to be lazy also lol), something to reflect on and learn. your examples uses the data i am working with so it makes it a little clearer to me. that might sound crazy to some but it does make it easier for me to understand lol. and thanks for yoru time and input sir :)