?
Solved

Matching input from keyboard to a file

Posted on 2003-02-24
3
Medium Priority
?
168 Views
Last Modified: 2010-03-05
Ask for input from the keyboard, if it is matched to a file in a particular directory, exit. If is does not match, looping for the input from the keyboard until it is matched.
Thanks in advance!
Andy
0
Comment
Question by:AndyDo
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 10

Accepted Solution

by:
rj2 earned 200 total points
ID: 8010527
#!/usr/bin/perl
opendir(THISDIR,"e:/temp") or die "Can not open directory: $!";
@allfiles = grep !/^\.\.?$/, readdir(THISDIR);
closedir(THISDIR);
for(;;) {
     print "Enter filename: ";
     $file=<STDIN>;
     chomp($file);
     foreach(@allfiles) {          
          if(lc($_) eq lc($file)) { #remove lc if you want case sensitive match
               exit;
          }
     }    
}
0
 
LVL 2

Expert Comment

by:blinkie23
ID: 8010605
rj2 is quicker than i am.  but here is my version.  i posted it because it allows the script to continue instead of just exiting.  give rj2 points, not me.

#!/usr/bin/perl

my $dirname = "/home/jon/bin";

opendir DIR,"$dirname" or die "$!";
my @files = grep /.*/, readdir DIR;

my $last = 0;
while($last != 1){
    print "filename? ";
    $_ = <STDIN>;
    chomp;
    foreach my $filetest (@files){
        if($filetest eq $_){ $last = 1; last; }
    }
}

print("matched $_\n");
0
 

Author Comment

by:AndyDo
ID: 8026989
Thanks for your help!!!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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

752 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