CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Variable

 



abhisri
Novice

Jun 21, 2006, 1:46 PM

Post #1 of 6 (904 views)
Variable Can't Post

Hello

I need to check for file names in a directory and I am using the following condition

my @files = grep (/\F*.pos$/i, readdir(DIR));
closedir(DIR);

foreach (@files)
{
$file = $_;
print $file
}


If I change the above script to the following it doesn't work...any help it would be great

$file_name = "F*.pos";

my @files = grep (/\$file_name$/i, readdir(DIR));
closedir(DIR);

foreach (@files)
{
$file = $_;
print $file
}


KevinR
Veteran


Jun 21, 2006, 4:26 PM

Post #2 of 6 (901 views)
Re: [abhisri] Variable [In reply to] Can't Post

this line:

my @files = grep (/\$file_name$/i, readdir(DIR));

should be:

my @files = grep (/$file_name$/i, readdir(DIR));

when you put the \ before $ "\$" perl treats the character after the \ literally. So perl thinks it's looking for "$file_name" instead of "F*.pos". The '*' in a regexp is not a wild card match if thats what you are thinking. It's a quantifier meaning zero or more.
-------------------------------------------------


abhisri
Novice

Jun 21, 2006, 5:32 PM

Post #3 of 6 (899 views)
Re: [KevinR] Variable [In reply to] Can't Post

Hello

Thanks for the reply...the filename will be "FCCWERD_20060615.pos" there will be lot many files but commonality will be FCC so I want to get a list of files from a directory which matches the criteria "FCC*.pos"....is there any better way to implement than this...

thanks again for the help

Sridhar


KevinR
Veteran


Jun 21, 2006, 11:11 PM

Post #4 of 6 (895 views)
Re: [abhisri] Variable [In reply to] Can't Post

use a glob and you can use wild card matching:

my @files = </dir/path/FCC*.pos>;

or with a regexp and grep:

my @files = grep {/^FCC.+\.pos/} readdir (DIR);
-------------------------------------------------


abhisri
Novice

Jun 22, 2006, 5:54 AM

Post #5 of 6 (888 views)
Re: [KevinR] Variable [In reply to] Can't Post

Hello Kevin

Thanks for the reply...just one more doubt

are both these same

grep {/^FCC.+\.pos/}";
grep {/\^$filename/};
where $filename = "FCC.pos";


KevinR
Veteran


Jun 22, 2006, 10:16 AM

Post #6 of 6 (885 views)
Re: [abhisri] Variable [In reply to] Can't Post

no they are not.
-------------------------------------------------

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives