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. ;-)


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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.