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: Regular Expressions:
Reading regex from a file...

 



grellsworth
New User

Apr 11, 2008, 1:17 PM

Post #1 of 8 (5472 views)
Reading regex from a file... Can't Post

given the line:

/home/user/images s#^(.*)\.jpg$#$1\.JPG#g

I would like to use opendir and readdir to generate an array of entries for the path /home/user/images and then use the regex to change each entry that ends with ".jpg" to "<whatever>.JPG".

My code follows

Code
sub regexrename 
{
my($basedir, $expr) = @_;

print "$basedir $expr\n";

opendir(DIR, $basedir);
my(@entries) = grep(!/^\.\.?$/, readdir(DIR));
closedir(DIR);

foreach $entry ( @entries )
{
chomp($entry);
$entry =~ $expr;
print "$entry\n";
}
}


Given that $basedir and $expr are being set to the proper values, why does this code not work?


Code
$basedir == "/home/usr/images" == true! 
$expr == "s#^(.*)\.jpg$#$1\.JPG#g" == true!


Any help is greatly appreciated.

Sincerely,

Gordon E.


KevinR
Veteran


Apr 11, 2008, 2:32 PM

Post #2 of 8 (5471 views)
Re: [grellsworth] Reading regex from a file... [In reply to] Can't Post

What is this line supposed to be?

$entry =~ $expr;

What is $expr?
-------------------------------------------------


grellsworth
New User

Apr 11, 2008, 3:08 PM

Post #3 of 8 (5467 views)
Re: [KevinR] Reading regex from a file... [In reply to] Can't Post


Quote
given the line:

/home/user/images s#^(.*)\.jpg$#$1\.JPG#g



In Reply To
If the above line is read from a file and then split at the space,
then the two parts are fed into the regexrename subroutine, something like this:


Code
regexrename($part[0], $part[1]); 

sub regexrename
{
my($basedir, $expr) = @_;

print "$basedir $expr\n";

opendir(DIR, $basedir);
my(@entries) = grep(!/^\.\.?$/, readdir(DIR));
closedir(DIR);

foreach $entry ( @entries )
{
chomp($entry);
$entry =~ $expr;
print "$entry\n";
}
}


then $part[0] would become $basename and $part[1] would become $expr, wouldn't it?


Quote
$basename == /home/user/images
$expr == s#^(.*)\.jpg$#$1\.JPG#g


Gordon E.



KevinR
Veteran


Apr 11, 2008, 3:42 PM

Post #4 of 8 (5465 views)
Re: [grellsworth] Reading regex from a file... [In reply to] Can't Post


Quote
then $part[0] would become $basename and $part[1] would become $expr, wouldn't it?


Yes it would but the regexp will not work like you have it. It would be better to use the qr operator to quote the regexp.

Untested code:


Code
my $line = '/home/user/images s#^(.*)\.jpg$#$1\.JPG#g'; 
@part = split(/\s/,$line);
$part[1] = qr{$part[1]};

regexrename($part[0], $part[1]);

sub regexrename
{
my($basedir, $expr) = @_;

print "$basedir $expr\n";

opendir(DIR, $basedir);
my(@entries) = grep(!/^\.\.?$/, readdir(DIR));
closedir(DIR);

foreach $entry ( @entries )
{
chomp($entry);
$entry =~ $expr;
print "$entry\n";
}
}


See if that works.
-------------------------------------------------


grellsworth
New User

Apr 11, 2008, 5:54 PM

Post #5 of 8 (5462 views)
Re: [KevinR] Reading regex from a file... [In reply to] Can't Post


Code
#!/usr/bin/perl -w 
my $line = '/home/user/images s#^(.*)\.jpg$#$1\.JPG#g';
@part = split(/\s/,$line);
$part[1] = qr{$part[1]};

regexrename($part[0], $part[1]);

sub regexrename
{
my($basedir, $expr) = @_;

print "$basedir $expr\n";

opendir(DIR, $basedir);
my(@entries) = grep(!/^\.\.?$/, readdir(DIR));
closedir(DIR);

foreach $entry ( @entries )
{
chomp($entry);
$entry =~ $expr;
print "$entry\n";
}
}


/home/user/images contents (BEFORE)

Quote
afile.jpg
Bfile.jpg
cFile.jpg
dfIle.jpg
efiLe.jpg


When executing the script, the only output I get is:


Quote
afile.jpg
Bfile.jpg
cFile.jpg
dfIle.jpg
efiLe.jpg


And when I check the directory contents again, they haven't changed... they are still all *.jpg (all lower-case).

I really appreciate your help so far!

Thank You,

Gordon E.


KevinR
Veteran


Apr 12, 2008, 12:11 AM

Post #6 of 8 (5455 views)
Re: [grellsworth] Reading regex from a file... [In reply to] Can't Post

If you want to change the names of files you have to use the rename() function. Reading the file into an array and changing them in the array only changes the names/variables stored in the array, not the file names stored on disk. Now that I think about it, I am not sure you can quote a substitution regexp s///, maybe qr only supports m//. But there is no need to pass the entire regexp into your function to rename jpg files to JPG. Just pass in the jpg extension and use that in a regxp.
-------------------------------------------------


grellsworth
New User

Apr 12, 2008, 3:32 PM

Post #7 of 8 (5450 views)
Re: [KevinR] Reading regex from a file... [In reply to] Can't Post


In Reply To
If you want to change the names of files you have to use the rename() function. Reading the file into an array and changing them in the array only changes the names/variables stored in the array, not the file names stored on disk. Now that I think about it, I am not sure you can quote a substitution regexp s///, maybe qr only supports m//. But there is no need to pass the entire regexp into your function to rename jpg files to JPG. Just pass in the jpg extension and use that in a regxp.



I grant you that I didn't put the rename commands in, and that's why the files didn't change, but the replace should have taken place in the variables at least, and that did not happen.

The point is not just to simply rename jpg files to JPG, but to allow the USER to use regex to change whatever files however they wish. What if some files were jpg, and some where jpeg and some were JpEg or whatever... regex would provide the power and flexability to rename whatever files the user wished, in a way that the user wanted to change them.

Gordon E.


KevinR
Veteran


Apr 12, 2008, 11:55 PM

Post #8 of 8 (5441 views)
Re: [grellsworth] Reading regex from a file... [In reply to] Can't Post

Personally I think you are going about it wrong. All you should pass to the script is the current directory where the files are (or chdir into it) and the old file extension and the new file extension, you should not be passing in a regexp, that part should be hard coded into the script.
-------------------------------------------------

 
 


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

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