Hi Everyone,
if my folder "foldera" inside has one file.
so if i do
if ($df =~ /^\./) { next; }
then i will get
###
filea
###
if i want to have
###
..
filea
###
means also display the parent .., how should i modify the perl ~// in my code?
Thanks
---------- Post updated at 06:49 AM ---------- Previous update was at 03:15 AM ----------
I figure out one way, i know it is not good, but it works.
if ($df =~ /^\.\./) {
This is Parent Directory
} elsif ($df =~ /^\./) {
} else {
This is files.
}
if you read a list of files into an array, then '.' is the first entry assuming nothing else is going on that I am not aware of. So just skip the first element of the array when displaying the file/folders:
open DIR, '.';
my @files = readdir(DIR);
for my $i (1..$#files) {
print "$files[$i]\n";
}
This should be more efficient than testing every element of the array with a regexp. But if you really need to test then use a string comparison operator instead of a regexp:
next if $df eq '.';
You don't need a regexp because you are not looking for a pattern.
Hi Kevin,
You mentioned "then '.' is the first entry assuming nothing else is going on that I am not aware of. So just skip the first element of the array when displaying the file/folders:", but sometimes let's say i have few files in a folder, when you print the file name, you may not always get the "." in the first, and ".." in the second line. So i may use sort first.
open DIR, '.';
my @files = readdir(DIR);
@new_files=sort @files;
for my $i (1..$#new_files) {
print "$files[$i]\n";
}
Do not know whether i thought correct or not.
If you sort the array first the . and .. could end up at other positions in the array, but when I did a test only three ASCII characters sort before . and ..
+
,
-
but your mileage could vary
You could try this:
open DIR, '.';
my @files = readdir(DIR);
@new_files=sort @files[1 .. $#files]; #leave out the first element of the array
for (@new_files) {
print "$_\n";
}