Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

(zdog) Re: removing x from a list

by zdog (Priest)
on Apr 20, 2001 at 07:30 UTC ( #74067=note: print w/ replies, xml ) Need Help??


in reply to removing x from a list

You can do this:

for (0 .. $#array) { if ($x == $array[$_]) { splice (@array, $_, 1); last; } }

Zenon Zabinski | zdog | zdog7@hotmail.com


Comment on (zdog) Re: removing x from a list
Download Code
Re: (zdog) Re: removing x from a list
by ashaman (Sexton) on Apr 20, 2001 at 08:05 UTC
    hrm . . . it does not seem to be working . . . perhaps some code is in order.

    opendir(SONGS, 'd:\my files'); @songs = readdir(SONGS); @songs = sort @songs; open(LIST, ">mp3.html"); print LIST<<End_Of_Html; Content-type: text/html <html> <head> <title>My MP3's</title> <center> Just a note of forwarning, there are over one thousand in here, so if +you intend to go through the whole thing, it may take you awhile. If + you want any of these, just ask and i can send it. Unfortunately i +have a very small hard drive and can't fit them all on this drive. <br> </center> End_Of_Html $x = '.'; $y = '..'; for (0 .. $#array) { if (($x == $songs[$_]) or ($y == $songs[$_])) { splice (@songs, $_, 1); last; } } print LIST join("<br>", @songs); close(LIST);
    basically, i'm trying to scan the folder with my mp3 archive and make a list of all of them, but exclude the directory special charcters '.' and '..' it isn't giving me any error or anything, it just keeps including the special characters.
      Methnks File::Find will be what you need to solve your real problem. This module - part of the standard Perl distribution IIRC - is designed to go through directories looking for files that meet certain criteria.
      You have to change the $#array in the for (0 .. $#array) to for (0 .. $#songs).

      Zenon Zabinski | zdog | zdog7@hotmail.com

        lol. my bad, i didn't notice that.
        lol, my bad, i didn't notice that. it still doesn't work, but thanks for pointing that out
      It might be a lot easier if you just got what you wanted out of the readdir call first:
      opendir (SONGS, $song_dir); @songs = sort grep { !/^\.\.?$/ } readdir(SONGS); closedir (SONGS);
      So in one line you get the sorted, filtered, directory listing. In this case, grep will only allow those array elements that are not '.' and '..' to pass through.

      Perl commands can be stacked, and the output of one flows right into the other, in a right to left manner. This means that your two statements:
      @songs = readdir(SONGS); @songs = sort @songs;
      Can actually be combined into a single equivalent one:     @songs = sort readdir(SONGS);
      Just as:
      $x = $y; $x = $z + $x;
      Can be converted to:    $x = $z + $y;

      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
Node Status?
node history
Node Type: note [id://74067]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2014-09-17 23:43 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (100 votes), past polls