Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
I am working with a directory that contains both data files and marker files. The marker files have the same base name as the data files but a different extension. They are used as markers that identify the data files that are no longer used. As a result, the files exist as pairs unless they are current in which case only the data file is present. An example directory listing would contain:

00000001.did 00000001.mrg 00000002.did 00000002.mrg 00000003.did

I want to work with all of the current data files so I am using the code below to find the list of data files that does not have a corresponding marker file.

sub unmergedFiles { my $dir = shift; my %merged; my @files; # create the hash containing the merged files foreach (glob ($dir."/*.mrg")) { if ( /(\S+)\.mrg/i ) { $merged{lc ($1 . ".did")} = "1"; } } foreach (glob ($dir."/*.did")) { unless (exists $merged{lc($_)}) { push @files, $_; } } # return style as per node 311537! return wantarray ? @files : \@files; }

This code works just fine but I would like the opinion of more learned monks as to how this code can be improved. A couple of things spring to mind:

  • There is probably a CPAN module to do this already but I haven't looked as writing the code was educational.
  • I am really just comparing the contents of two arrays to find the difference. Have I missed out on a more concise method?
  • I am creating a hash by iterating over an array and assigning new hash elements. Is there a better way of doing this?

All comments gratefully received!

inman


In reply to Finding un-paired files in a directory by inman

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2024-04-25 09:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found