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;

In reply to Re^2: Compare and copy array values by Narveson
in thread Compare and copy array values by tgolf4fun

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.