in reply to Search for text from user input
As others have said, it's a good idea to use warnings and use strict, always. This would be easy to modify to make it accept input on the directory as well. The i flag at the end of the regex ( /pattern/i ) makes it match case-insensitive. That way you do not need to specify JPEG, Jpeg, jpeg, and so on...#!/usr/bin/perl use strict; use warnings; print "Please enter text for string search: "; my $search=<STDIN>; chomp $search; opendir(DIR, ".") or die "Cannot open dir ($!)\n"; while (my $file = readdir(DIR)) { print "$file\n" if ( $file =~ /$search\.jpg|$search\.jpeg/i ); } closedir(DIR); exit 0;
|
|---|