in reply to Re: Compare and copy array values
in thread Compare and copy array values

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;