I am working on a script and cannot figure out how to get this last part working.
I have two arrays that I need to compare and then do something with the comparison.
The first array is a list of files. @update
The second array is a list of directories.@prod
I created a hash out of the second array (directories). Now the comonality between the two arrays is that the first 4 characters of the file array will match the name of the directories. I have been trying to write a loop that would look at the first 4 characters of each item in the file array and compare it to each item in the directory array. When it finds an item that matches it is to copy this item to the corresponding folder (the folder who's name matches the first 4 character of the file name).
So far as stated I have a list of the arrays and then turned the array of directories into a hash, then come my problem. Comparing the array of files to the hash is not working as expected. I know I am using "if" in a loop with a hash, is this incorrect? Here is what I have tried:
So this opens the array of directories and assignes them to a hash.
opendir (DIR2,"$to_dir") || die "Cannot open directory $!";
@prod=readdir(DIR2);
closedir(DIR2);
@prod = sort @prod;
foreach $h(@prod){$seen{$h} = 1}
This creates an array of files and then incorrectly try to compare the first 4 items of the array to match an item in the hash and then copy it. I can figure out the copy part, but hash parsing escapes me.
opendir(DIR,"$from_dir") || die "Cannot open directory $!";
@update=readdir(DIR);
closedir(DIR);
@update= sort @update;
foreach $i(@update){
if (/^\d{4}/ eq $seen{$h}){
copy($i,$to_dir)
}
Any pointers would be greatly appreciated.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.