Hi, I have some advice for you:
  1. When defining an empty list, do not assign () to it, it is a waste of code (as a list is ALWAYS defined, but empty, at creation time)
  2. Use exceptions. Your functions (which aren't commented much, as to what they expect, or what they produce, by the way) simply return if something goes wrong. Try using
    die "Error message for the user"
    instead.
  3. Check arguments BEFORE going into functions, not after those functions failed. It makes more sense to check ahead that you have what is needed (and display your usage message if that's not the case) than try anyway, and then react if it failed.
  4. In your first_arg and second_arg functions, your "next if -d $file" doesn't seem to make much sense, to me. Do you really want to skip the file or directory names matching subdirectories of your current directory ?
  5. Avoid using the same name for different things, it gets confusing. Having an array and a sub share the same name works fine with Perl, but you'll have more chances of making a mistake yourself. Naming the array @first_arg, and the sub get_first_args, for instance, would be cleaner.
  6. Your first_arg and second_arg functions do exactly the same thing, except they fill different lists. This is what references are for, you know: Either give the reference to the array you want to fill as a parameter, or define an array IN your sub, then return its reference (and work with it after). It is much cleaner (to get a ref of something, just put a '\' in front of it. For instance, \@first_arg is a ref to @first_arg. To get the thing back from its ref, just prefix the ref value with the type, for instance, if $ref holds the ref to an array, then @$ref is the array. Or @{$ref}, if you prefer (or have multiple reference levels)
  7. When using exceptions (die), you do not need to print the line number yourself, it is automatically added by Perl if you do not add "\n" to your message (and more up to date, for instance, there are more than 11 lines between your "line 90" and "line 101" messages...)
  8. Use hashes, to regroup things that belong together. In your @dex_md5_array, you put strings regrouping three elements: The MD5, the directory, and the file name. You should, instead, put these in a hash, and put a reference to this hash in the array. For instance, you could write
    push @myArray, {'md5' => $md5, 'dir' => $dir, 'file' => $fileName};
    And to retrieve something from the hash reference, just write, for instance ${$hashRef}{'md5'}, or whatever. This way, you do not have to parse them again later. Oh, and chomp them when you put them in, not when you use them (it's much better to have clean values, than having to clean them each time you want to use them).
  9. Your element1 and element2 loops are the same, with only the source and destination arrays changing. Make a sub, and use references, you'll be glad you did.
  10. When comparing the MD5 of the files you wish to copy...
    • Do you really wish to compare MD5 first, when you're really wanting to look for files having the same name ?
    • Can different files have the same base name, but be in different directories, on the same "side" ? And, if so, do you really wish for the last file with that name to overwrite ALL the files with the same name in the destination directories ?

In reply to Re^2: File copy based on conditions in two arrays by kzwix
in thread File copy based on conditions in two arrays 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.