Re: Reading Directory

2002-09-05 Thread Michael Lamertz

On Wed, Sep 04, 2002 at 09:41:54PM +0200, Fabian Funk wrote:
> 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))) {

Here you assign to $file...

>   next if $file=~ /^\.\.?$/;

and here you use it...

>   next unless /\.[mp3]$/i;

but here you use $_ instead.
Besides that, you're matching all files *.m, *.p and *.3.  Match for
/\.mp3$/ instead.

>   push(@files,$file); 
> 
> But i get "Use of uninitialized value in pattern match (m//)
> What's wrong ???

That was the match on $_ which hadn't been set.

BTW:  perldoc -f readdir has a pretty good example for what you need to
do.

Try replacing your whole while construct with 

my @files = grep { -f $_ && /\.mp3$/ } readdir DIR;

-- 
Well, then let's give that Java-Wussie a beating... (me)

Michael Lamertz| +49 2234 204947 / +49 171 6900 310
Sandstr. 122   |   [EMAIL PROTECTED]
50226 Frechen  | http://www.lamertz.net
Germany|   http://www.perl-ronin.de 

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Reading Directory

2002-09-05 Thread Janek Schleicher

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]




RE: Reading Directory

2002-09-05 Thread Jeff AA

Why not:

  my $spec = "/var/www/*.mp3";
  my ( @files ) = glob $spec;
  print "Checked '$spec' - found ", scalar(@files), " files:\n  ",
join("\n  ", @files ), "\n";

regards
Jeff


> -Original Message-
> From: Fabian Funk [mailto:[EMAIL PROTECTED]] 
> Sent: 04 September 2002 20:42
> To: [EMAIL PROTECTED]
> Subject: Reading Directory
> 
> 
> 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;
>   push(@files,$file); 
> 
> But i get "Use of uninitialized value in pattern match (m//)
> What's wrong ???
> 
> 
> -- 
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
> 


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Reading Directory

2002-09-05 Thread Omanakuttan

opendir DIR, 
while (defined(my $file=readdir(DIR))) {
next unless $file=~/\.mp3$/i ;
push(@files,$file); 
}

works for me.



Omanakuttan N,
C,C++,Networking, perl, Linux
Tata Elxsi Ltd,
ph.8410 222 x 414.
---
'   ...follow me' , the wise man said, but he walked behind ...




-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]




Re: Reading Directory (fwd)

2002-09-05 Thread Sudarshan Raghavan



Forgot to forward to the list, sorry again

-- Forwarded message --
Date: Fri, 6 Sep 2002 15:19:58 +0530 (IST)
From: Sudarshan Raghavan <[EMAIL PROTECTED]>
To: Fabian Funk <[EMAIL PROTECTED]>
Subject: Re: Reading Directory

On Wed, 4 Sep 2002, Fabian Funk wrote:

> 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;

You have to read the filename into $file and matching on $_ (pattern match 
defaults to this). Also this matches for *.m or *.p *.3.

You can do away with the while loop
my @mp3_files = <$your_search_dir/*.mp3>;
perldoc -f glob
perldoc File::Glob
 



-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]