in reply to comparing two lists

this works for me:

use strict; open(PLAYLIST, "<playlist.m3u"); opendir(FILES, 'd:\my files'); my %playlist ; my @filelist = grep {/\.mp3$/} readdir FILES; my $song; while(<PLAYLIST>) { chomp; if(not(/#/)) {$playlist{$_} = 1} } foreach $song(@filelist) { if(!exists $playlist{$song}) { unlink($song) } } closedir FILES; close PLAYLIST;

hashes are sweet. (and notice the chomp)

anders pearson

Replies are listed 'Best First'.
Re: Re: comparing two lists
by ashaman (Sexton) on Jun 24, 2001 at 08:28 UTC
    hm . . . ok, that works, except for the part where it deletes the file. it's weird. if i change unlink($song) to, say, print $song, it prints the filenames. but for some reason, unlink($song) doesn't work
      If you use print $song, it prints the filenames, but not the full paths, right? Since the file is not in the current working directory, you need to use the path with the filename so that Perl knows where to find the file.
      my $dir = 'd:\my files'; opendir(FILES, $dir) or die "Can't opendir '$dir': $!\n"; # read in the playlist and the files, create the hash, and so on... foreach $song(@filelist) { if(!exists $playlist{$song}) { unlink("$dir/$song") or warn "Can't unlink '$dir/$song': $!\n"; } }