in reply to Compare and copy array values

If I understand correctly, the keys of the hash %seen will be the directory names, so what you need to compare is the filename to those hash keys. What you are doing now is compare the filename to the value of one hash key.

what you want is to check if the result of the pattern match is a directory that has been seen.

foreach $i(@update){ $i =~ /^(\d{4})/; # match and grab into $1 the first four digit +s if($seen{$1} == 1){ # test if the dir has been seen copy($i,$to_dir); } }

Replies are listed 'Best First'.
Re^2: Compare and copy array values
by Narveson (Chaplain) on Mar 19, 2008 at 20:14 UTC
    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).

    Sounds like your desired folder is a subfolder of $to_dir. Instead of

    copy($i,$to_dir);
    assign that four-character string to a variable so you can
    1. check for the existence of a subfolder with that name, and
    2. copy the file to that subfolder.

    The capture variable $1 holds the string you want if the match succeeded, otherwise it may hold something left over from an earlier successful match. So don't use $1. Say

    FILE: for my $file (@update) { my $first_4_chars = substr($file, 0, 4); next FILE if not $seen{$first_4_chars}; copy($file, "$to_dir/$first_4_chars"); }

    Oh, and please

    use strict; use warnings;