Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

(dkubb) Re: (3) removing x from a list

by dkubb (Deacon)
on Apr 20, 2001 at 09:03 UTC ( [id://74088]=note: print w/replies, xml ) Need Help??


in reply to Re: (zdog) Re: removing x from a list
in thread removing x from a list

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;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://74088]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (1)
As of 2024-04-25 01:19 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found