in reply to Re^3: Using an hash ref to match files with directories
in thread Using an hash ref to match files with directories

NetWallah,
I need to copy files from one dir to another. The directories are on two
separate machines across a file share. The files names start with initials
followed by an underscore, then some other characters. I need to put
these files into their respective customer dir.

The customer dir name is the first and last name of the
customer. Example: Michael Jackson. So, in order to copy
a file/s that belong to Michael Jackson I need to match
his initials with his name. That is where the %sequser
hash comes in. It creates a mapping between the initials and name.

My idea is to match the initials of the customer, the values in the hash,
with the initials at the beginning of the file. Then I will know which user
dir to copy the file.

I hope that I have significantly decreased the signal-to-noise ratio.
#!/usr/bin/perl -w use strict; use DateTime; use File::Copy; use File::Find; use File::stat; use Time::localtime; use Data::Dumper; use File::Glob ':glob'; 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: \"\\\\Data\\Sequencing_Results\\RESULTS $ye +ar\\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 initials of the user at the # beginning of the sequence file. After the match is made then # the file/s are copied to the users folder my %sequser = ("MJ"=>"Michael Jordan", "LJ"=>"Lebron James", "KB"=>"Ko +be Bryant"); 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","$results_dir") 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; } } } }

LomSpace

Replies are listed 'Best First'.
Re^5: Using an hash ref to match files with directories
by NetWallah (Canon) on Aug 12, 2009 at 06:09 UTC
    Thanks - that is a significantly improved S/N ratio.

    First : Why are you doing this no-op ?

    my $seq = $seq; # capture the intials ($seq) from $targetfile

    So it appears that you want to use the Initials-to-hash conversion in the sub "seq_customer_dirs".

    Simply replace " if ($dir=~m%$seq%){" with

    if ($dir =~m|$sequser{$seq}| ){

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

      first response:
      This captures the initials from the targetfile. Those initials
      are what I need to check for a match with the keys in
      in the hash. Once I find a match, then I can use the value to
      work on the correct dir.

      Second response:
      The idea behind the sub seq_customer_dirs is to find the customer/user dir.
      Example: When the program runs across a file with the initials "MJ",
      which is captured by $seq, it checks for eq with a hash $key.
      If there is eq, hash $key = "MJ", then the value is returned as the dir.
      In this case the $value is "Michael Jordan" and that would be returned
      to the main program as the
      directory to work on.

      LomSpace
Re^5: Using an hash ref to match files with directories
by ww (Archbishop) on Aug 12, 2009 at 06:51 UTC
    And do you have a plan of action ready for the day when Marvin Jones and Mary Johnson join the late Mr. Jackson on your customer list?
      "Mjon"=>"Marvin Jones", "MJoh"=>"Mary Johnson" will be added to the hash.