Sometimes it's easier just to say what you will allow, and exclude everything else, than to exclude a multitude of patterns. You're likely to miss something in the process. This statement holds true in everything from security, user input validation, and to your specific problem.
Since you are making a list of mp3 files, you could only allow files with the extension .mp3 to get into your @songs array, and exclude all others.
Also, it's usually a good idea to sort after you have removed as much as possible from your list. Why bother to sort something that could have less elements in the near future? =) The original sample code you posted reads the directory, copies the file names into an array, then sorts the array and copies it back again to the same array.
Of course, if efficiency was really important, I wouldn't have used grep or a regex, but I think this illustrates the point:
#!/usr/bin/perl -wT use strict; use constant SONG_DIR => 'd:\my files'; opendir(SONGS, SONG_DIR) or die "could not open the ", SONG_DIR, " directory: $!"; my @songs = sort grep { /\.mp3$/ } readdir SONGS; closedir SONGS;
In reply to (dkubb) Re: (3) removing x from a list
by dkubb
in thread removing x from a list
by ashaman
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |