Sorting of file name into an array using Perl and Regex

sub open_directory {
    my $directory = shift @_;
    my @files = ();

    opendir (my $dh, $directory) or die "Couldn't open dir '$directory' : $!";
    my @all_files = readdir $dh;
    closedir $dh;

    foreach my $files(@all_files){
            while($files =~ /\.htm/){
    return @files;

The error is at the code push(@files); The error is : Useless use of push with no values

I want to process the files with the the name ending of .htm or .html in the @files array using the regex /\.htm/ please help me.


The most simply way to solve this is by using the grep builtin: It selects those elements from a list where a condition is true, then returns the list of all matching elements E.g.

my @even = grep { $_ % 2 == 0 } 1 .. 10; # even number in the interval [1, 10].

In our case, we can do

my @files = grep { /\.htm/ } readdir $dh;

If you want to use push, then you (a) have to specify what you want to push onto the array, and (b) should only do the pushing if the regex matches, not while it matches:

for my $file (@all_files) {
  push @files, $file if $file =~ /\.htm/;

Need Your Help

How to get the onfocus event to fire for checkbox in firefox and chrome

javascript visual-studio-2010 firefox web-applications

In IE i am using an onfocusin event and everything works as expected for the checkbox however onfocusin only works with IE and not other browsers. Also the onfocus doesn't work for checkbox's eithe...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.