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

In reply to Using an hash ref to match files with directories by lomSpace

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.