Zoop has asked for the wisdom of the Perl Monks concerning the following question:
There are two directories(for eg. alpha and gamma), which contain numerous xml files . each xml file in alpha corresponds to a file in gamma directory . The naming convention of these two files in two directories have a pattern for eg
File in alpha -> abc_12342_tick.xml.alphaprodFile in gamma -> abc_12342_tick.xml.gammatest
What I am trying to achieve is a hash with these file names paired. here is what I did. @alphafiles and @gammafiles contains the file list from alpha and gamma directory.
This code works fine for a smaller set of files in these directories but for a large set of files the number of iterations increases and it becomes extremely slow like takes 90 mins to pair up 30000 files in each directory. I also tried a simpler approach of sorting @alphafiles and @gammafiles lists first and then pairing them like below@hash{@alphafiles}=(); foreach (keys %hash){ my $patt=substr($_,0,index($_,"xml")); @matched=grep(/^$patt/,@gammafiles); $hash{$_} = $matched[0]; }
This method takes hardly a couple of minutes but I doubt the reliability of this approach.I need to make sure I pair the correct files from the two directories.I seek your wisdom. Is there a way to apply a regex during sorting of the arrays which yields files at the correct positions before clubbing?@sortalpha= sort @alphafiles; @sortgamma= sort @gammafiles; $i=0; while($i <= $#sortprod) { $hash{$sortalpha[$i]}=$sortgamma[$i]; $i++; }
Thanks in advance
'When You starve With A Tiger, The Tiger always starves last'
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: File pairing from different directories
by Corion (Patriarch) on Sep 07, 2009 at 11:56 UTC | |
by Zoop (Acolyte) on Sep 08, 2009 at 13:30 UTC | |
by Corion (Patriarch) on Sep 08, 2009 at 13:35 UTC |