Fabian Funk wrote at Wed, 04 Sep 2002 21:41:54 +0200: > Hi, I'm pretty new to perl. I want to open a Directory with mp3 files an > other files. My Poblem is i need only the mp3 files. How can i get only > the mp3 files and not other files ? > > I tried: > > while (defined(my $file=readdir(DIR))) { > next if $file=~ /^\.\.?$/; > next unless /\.[mp3]$/i; ^ ^
The [ ... ] create a character class, so the regexp will match files like *.m or *.p or *.3. Just delete the '[' ']': next unless /\.mp3$/i; > push(@files,$file); > > But i get "Use of uninitialized value in pattern match (m//) > What's wrong ??? You forgot to say that you want to check $file, so the regexp is matched against $_ what is unitialized in your case. write instead: next unless $file =~ /\.mp3$/i; However, you also could shorten your script to: my @mp3_files = grep /\.mp3$/i, (readdir DIR); Note that you don't need the check for '.' or '..' files, as these don't end on .mp3. Note also that the one liner makes problems with huge directories (perhaps 100_000 files). Greetings, Janek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]