Finding directories from a list (case-insensitive)

Posted on 2007-10-04
Last Modified: 2010-03-05
1st off, I started looking into using Perl just a few days ago.  So I'm quite the noob at all of this.  My boss really wants me to use Perl,  so here I am.

I need to check to see if certain sub directories exist by comparing them to a list of possible names.

Users might change the case or abbreviate certain parts of the directory from the standard name.
So dir_one might end up being named Dir_one, dir_ONE, DIR_ONE or even dir_1....
( I can handle the dir_1 case as an exception later in the code it that helps)

My results should be a list of valid directories as well as having a list of missing directories.
I've currently borrowed some code from Colin that checks two arrays and returns duplicates and differences:

% perl -Mstrict -Mwarnings
sub find_dups($$){
  my %hash1 = map { $_=>1 } @{+shift};
  return grep { $hash1{$_} }  @{+shift};

print join(', ', find_dups([qw(a c d e f g h)], [qw(g f b e)])), "\n";

Gives => g, f, e

And this one that finds differences:

sub find_uniques($$){
  my %hash1 = map { $_=>1 } @{+shift};
  my %hash2 = map { $_=>1 } @{+shift};
  my @notin2 = grep { !delete($hash1{$_}) } keys(%hash2);
  return @notin2, keys(%hash1);
print join(', ', find_uniques([qw(a c d e f g h)], [qw(g f b e e e e)])), "\n";
    Which returns b, h, a, c, d as expected.

I need to have something that ignore case in the compare but save the valid names at the end.


Question by:ISGDude
    LVL 84

    Accepted Solution

    sub find_dups($$){
      my %hash1 = map { (lc) =>1 } @{+shift};
      return grep { $hash1{+lc} }  @{+shift};

    Author Comment

    OK that works.. which leads me to more noob issues

    print join(', ', find_dups([qw(a c d e f g h)], [qw(G F d e)])), "\n";
    prints out "d G F"  

    @list1 = qw(a c d e f g h);
    @list2 = qw(G F d e) ;
    print join(', ', find_dups(   @list1, @list2   ), "\n";
    returns nothing...????

    Also, I'd like to capture the results into an array or string.  
    I seem to be having trouble there too.

    (These are the last add-on questions, I promise)

    LVL 84

    Expert Comment

    print join(', ', find_dups(  \@list1,\ @list2   ), "\n";
    print join(', ', find_dups(  [@list1], [@list2]   ), "\n";
    LVL 84

    Expert Comment

    $result = join(', ', find_dups([qw(a c d e f g h)], [qw(G F d e)]));
    print $result,"\n";
    LVL 84

    Expert Comment

    perldoc -q intersection
    perldoc -q duplicate
    perldoc perldata
    perldoc perlsub

    Featured Post

    How your wiki can always stay up-to-date

    Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
    - Increase transparency
    - Onboard new hires faster
    - Access from mobile/offline

    Join & Write a Comment

    On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now