lomSpace has asked for the wisdom of the Perl Monks concerning the following question:

Hello,
I am using a hash and a hash reference. The hash contains key => value pairs that contain
a customer's first name last name => initials. The hash reference contains the files =>
customer's initials.

I need to match the values from the hash and hashref, initials, to locate the customer name, which
is the key in the hash, and then deposit the files in their dir. The files are located in the hashref keys.
#!/usr/bin/perl use strict; use DateTime; use File::Copy; use File::Find; use File::stat; use Time::localtime; use Data::Dumper; use File::Glob ':glob'; use Log::Log4perl qw(get_logger :levels); Log::Log4perl->init("find_file_log.conf"); our $LOGGER = Log::Log4perl->get_logger(); $LOGGER->info("my app started"); my $now = DateTime->now(time_zone => 'floating'); my $yesterday = $now->subtract(days=>1); my $year = $yesterday->year; my $month = $yesterday->month; my $month_abbr = uc($yesterday->month_abbr);# uppercase string chars my $day = $yesterday->day; my $year_abbr = substr "$year",2,2; # location of files to be processed. my $root="C:\\ab3730\\SeqData\\RUN$month/$day/$year_abbr"; print $root,"\n"; # location of seq customer directories #map drive for RESULTS directory. Unable to reference this directory d +irectly. system ("net use i: /delete"); my $err=system("net use i: \"\\\\tarfs-orion\\Data\\Sequencing_Results +\\RESULTS $year\\New Version $year\""); if ($err!=0){ $LOGGER->logdie("unable to map RESULTS directory on orion."); } # This hash reference holds the names of the sequencing core cust +omers. # The values should match the the initials of the user at the # beginning of the sequence file. After the match is made then # the file/s are copied to the customers folder my %sequser = {Abigail McPherson => AM, Ana Kostic => AK, Anna Zengin +=> AZ, Anu Jain => AJ, Ashwin Ricci=> AR, Brinda Prasad=>BP, Carlos Arrecubieta=> CA, Catherine +Walsh=> CW, Charleen Hunt=> CH, Claudia Canasto=> CC, Daniel Brims => DBr, Darya Burakov=> DBu, David Heslin + => DH, Dawn Thurston => DT, Dipali Nimbalkar => DN, Erin Allison => EA, Evana Sirabian => ES, Evangelos Pe +fanis => EP, Evin Feldman => EF, Frank Delfino => FD, Gleb Sagitov => GS, Gregg Warshaw => GW, Hans Gartner +=> HG, Inessa Rivkis => IR, Jamie Orengo => JO, Jason Pitts => JP, Jason Yasenchak => JY, Jaya Ahuja = +> JA, Jean Yanolatos => JYa, Jee Kim => JK, Jennifer Griffiths => JG, Jie Cao => JC, Jody Ann Well +er => JAW, Joelle Carlo => JoC, John McWhirter => JMcW, Joseph Quispe => JQ, Joyce McClain => JMcC, Julie Ritc +he => JR, Kara Olson => KA, Katie Yang => KY, Kieran Feeley => KF, Kihwa Kang => KK, Kristen Bloom = +> KB, Leela Raj => LR, Maggie Zhong => MZ, Mansi Parikh => MPa, Marc Morra => MM, Matthew Pendlet +on => MPe, Michel Naum => MN, Min Gao => MG, Nina Schilt => NS, Peter Gasparini => PG, Peter Lengye +l => PL, Qin Liu => QL, Richard Corpina => RC, Richard Welsh = > RW, Rishi Mahajan => RM, Robert Babb + => RB, Ron Deckelbaum => RD, Roxanne Ally => RA, Sarah Hatsell => SH, Sharon Okenquist => SO, Steven Ir +vin => SI, Tara Young => TY, Uzodinma Uche => UU, Vera Voronina => VV, Vincent Idone => VI, Yang Wei => +YW, Yu Zhao => YZ}; my $seq_user_dirs ="i:"; my %files=(); find(\&wanted, $root); #check the arrays in the hash looking for seqs with atleast a file foreach my $k (keys(%files)){ $LOGGER->debug("working on $k"); #get the array from the array reference. my @arr=@{$files{$k}}; #skip unless we have atleast 1 file in the array + next unless scalar @arr >= 1; my $seq_user_base = seq_customer_dirs($k); if (!defined $seq_user_base || $seq_user_base eq ''){ $LOGGER->error("Unable to find seq user dir for: $k"); next; } my $results_dir=$seq_user_base."\\"."Results_".$month."-".$day."-".$ +year; $LOGGER->debug("Creating Results dir: $results_dir"); mkdir $results_dir, 754; #now iterate through the array, copying the files to the folder base +d on # the name foreach my $f (@arr){ copy("$f","$user") or $LOGGER->info("unable to copy $f"); $LOGGER->info("copying $f"); } ################################ Subroutines ######################## +######## sub wanted { # This method traverses $root and locates the sequence files # It processes them based on date modified, in our case less than a da +y my $targetfile = $File::Find::name; # Here we locate the files to + be copied return if (-M $targetfile > 1.0); # return the files that are +less than a day old my ($seq) = $targetfile=~ /.*\/(\D+)_.*/; # capture the initials f +rom the $targetfile my $seq = $seq; # capture the intials ($seq) from $targetfile # create a hash of $targetfile keys and the captured $seq values + push @{$files{$seq}}, $targetfile ; } sub seq_customer_dirs{ # Here we look to match initials with seq customer directories. Key va +lue pairs are set up in # a hash so that the initials can match the value and then copy to the + correct directory based # on the key. If we find a match then we return it to the main program +ming for processing. my $seq=shift; my @dirs = glob($seq_user_dirs."\\*"); foreach my $dir(@dirs) { if (-d $dir){ if ($dir=~m%$seq%){ return $dir; } } } }

I could use some of that good ol' perl wisdom.
LomSpace

Replies are listed 'Best First'.
Re: Using an hash ref to match files with directories
by NetWallah (Canon) on Aug 08, 2009 at 00:03 UTC
    Not exactly clear in your problem definition, but here are a few starting points:

    • use warnings. This would have told you "Reference found where even-sized list expected at ..." - You have a problem with the declaration of %sequser - use round brackets.
    • Keys and values for %sequser seem to be reversed for what you apparently want to do. You can use the 'reverse" function, after you fix point 1.
    • Where does "$user" come from ?
    • It would help if you showed the source and target directory structure samples.

         Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)

      Hi NetWallah!
      I meant "seq_user_base". The source and target directories are:
      $root and the perl system call immediately following it.
        Unfortunately, your telepathic transmissions have a very poor signal-to-noise ratio, and even with reception tuned to the highest sensitivity, I am regrettably unable to decode them.

        Please describe your problem better, and also let us know the results of following the suggestions you have received.

             Potentia vobiscum ! (Si hoc legere scis nimium eruditionis habes)