Sorting of file name into an array using Perl and Regex -
sub open_directory { $directory = shift @_; @files = (); opendir (my $dh, $directory) or die "couldn't open dir '$directory' : $!"; @all_files = readdir $dh; closedir $dh; foreach $files(@all_files){ while($files =~ /\.htm/){ push(@files); } } return @files; }
the error @ code push(@files);
error : useless use of push no values
i want process files the name ending of .htm
or .html
in @files
array using regex /\.htm/
please me.
the way solve using grep
builtin: selects elements list condition true, returns list of matching elements e.g.
my @even = grep { $_ % 2 == 0 } 1 .. 10; # number in interval [1, 10].
in our case, can do
my @files = grep { /\.htm/ } readdir $dh;
if want use push
, (a) have specify want push onto array, , (b) should pushing if regex matches, not while matches:
for $file (@all_files) { push @files, $file if $file =~ /\.htm/; }
Comments
Post a Comment