in reply to Files and Directories that share a regex variable

Based on what you shown and said so far, I can't figure out what the starting conditions and task really are.

I have files and directories that share a regex. For every directory there are 2-4 files that share an id number. Example:

Directory: 11355 Zfp71
files: 11355_SU, 11355_SD, 11355_pGk, 11355_LacZ

The files and directories exist in different different directories.

Do you mean that you have a set of files that all share the same initial substring, and that somewhere else you have a directory whose name also shares that same substring? Is the common initial substring always a string of digits followed by underscore?

Is it the case that the matching directory already exists, or do you need to create new directories at run-time (using mkdir), based on the set of file names that you find, in order to have a place for files that share a given substring? And is it really a matter of copying (so you end up with identical files in two places), or do you actually want move the files (keeping only one copy of each file, and just changing which directory it is in)?

Looking at your code, I don't see you using opendir and readdir (which I think you'll need to use), or File::Find (which might not be the right thing for what you want to do).

So far, you are just assigning a single value (a path that ends with "/SequenceAssemblies") to a single hash key (which is just like the value, but without the "/SequenceAssemblies" part), and then you are doing a "sort" and a "for" loop on that single hash key, which seems pointless. (Update: also, this is being done in a subroutine that never gets called by the main part of the script.)

Also, the terms "primer sequence file" and "maid folder" don't make sense in your code comments; what are you really talking about there?

If the task is to look at the files found in one directory, and then sort them out for copying into separate directories based on matching initial digit strings, I think something like this might be a place to start (not tested):

use strict; use warnings; use File::Copy; my $source_path = "/some/path"; my $sorted_path = "/some/other/path"; # get the set of existing sorted directories: my %sorted_dir; opendir( SORTED, $sorted_path ) or die "$sorted_path: $!\n"; while ( my $name = readdir( SORTED )) { if ( -d "$sorted_path/$name" and $name =~ /^(\d+_)/ ) { my $dir_key = $1; $sorted_dir{$dir_key} = $name; } } closedir SORTED; # go through the files to be sorted: opendir( SOURCE, $source_path ) or die "$source_path: $!\n"; while ( my $file = readdir( SOURCE )) { my $src = "$source_path/$file"; next unless ( -f $src and $file =~ /^(\d+_)/ ); my $target_key = $1; if ( not exists( $sorted_dir{$target_key} )) { warn "No directory exists in $sorted_path to hold $file\n"; next; } my $dest = "$sorted_path/$sorted_dir{$target_key}/$file"; if ( -f $dest ) { warn "$src already exists as $dest; not copied\n"; next; } copy( $src, $dest ) or warn "failed to copy $src to $dest: $!\n"; } closedir SOURCE;
This is not trying to create sorted directories if they don't exist yet, and it assumes that if a file already exists in a target/sorted directory, the copy operation should not be done (because either the copy is already there, or else you might lose some useful data if an existing file is replaced by a new and different one). If you want different behavior, I'll "leave that as an exercise"...