Hello Monks,

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.alphaprod

File 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.

@hash{@alphafiles}=(); foreach (keys %hash){ my $patt=substr($_,0,index($_,"xml")); @matched=grep(/^$patt/,@gammafiles); $hash{$_} = $matched[0]; }
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

@sortalpha= sort @alphafiles; @sortgamma= sort @gammafiles; $i=0; while($i <= $#sortprod) { $hash{$sortalpha[$i]}=$sortgamma[$i]; $i++; }
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?

Thanks in advance

/zoop

'When You starve With A Tiger, The Tiger always starves last'


In reply to File pairing from different directories by Zoop

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.