Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Finding directories from a list (case-insensitive)

Posted on 2007-10-04
Medium Priority
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
  • 4
LVL 85

Accepted Solution

ozo earned 2000 total points
ID: 20016306
sub find_dups($$){
  my %hash1 = map { (lc) =>1 } @{+shift};
  return grep { $hash1{+lc} }  @{+shift};

Author Comment

ID: 20018051
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 85

Expert Comment

ID: 20018081
print join(', ', find_dups(  \@list1,\ @list2   ), "\n";
print join(', ', find_dups(  [@list1], [@list2]   ), "\n";
LVL 85

Expert Comment

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

Expert Comment

ID: 20018124
perldoc -q intersection
perldoc -q duplicate
perldoc perldata
perldoc perlsub

Featured Post

Become an Android App Developer

Ready to kick start your career in 2018? Learn how to build an Android app in January’s Course of the Month and open the door to new opportunities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
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 (http://dilbert.com/strips/comic/2007-08…
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…
Six Sigma Control Plans
Suggested Courses

564 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