variable interpolation failed :-(

F

fred

Hi !

I trie to remove the extention of a file name.
The folowing code work well :

$nom_fichier_final =~ s/pdf// ;

but the folowing one failed and I do not
understand why :-( Indeed according to the
PERL documentation, $motif should be replaced
by pdf during compilation...

my $motif = 'pdf' ;
$nom_fichier_final =~ s/$motif// ;

Thank you for your help !
 
P

Paul Lalli

Hi !

I trie to remove the extention of a file name.
The folowing code work well :

$nom_fichier_final =~ s/pdf// ;

but the folowing one failed and I do not
understand why :-( Indeed according to the
PERL documentation, $motif should be replaced
by pdf during compilation...

my $motif = 'pdf' ;
$nom_fichier_final =~ s/$motif// ;

Thank you for your help !

Those two code segments do exactly the same thing. If one did what you
wanted, so shoudl the other. Therefore, you're either not showing us
you're actual code, or there is something else wrong with your program
that you've discounted as the possible cause of your bug. Post a *short
but complete* program demonstrating the problem you're seeing.

BTW, it's "Perl", not "PERL". And also, if you really want to remove the
..pdf extension, wouldn't you want a regexp of
s/\.pdf$//;
rather than what you have?

Paul Lalli
 
F

fred

I just come to find the error : there was a blanck just after $motif and I
did nit see it ! :)

Le Wed, 21 Apr 2004 10:17:43 -0400, Paul Lalli a écrit :
[texte rapporté caché]

Those two code segments do exactly the same thing. If one did what you
wanted, so shoudl the other. Therefore, you're either not showing us
you're actual code, or there is something else wrong with your program
that you've discounted as the possible cause of your bug. Post a *short
but complete* program demonstrating the problem you're seeing.

BTW, it's "Perl", not "PERL". And also, if you really want to remove the
.pdf extension, wouldn't you want a regexp of
s/\.pdf$//;
rather than what you have?

Paul Lalli
 
J

Jürgen Exner

[...]
And also, if you really want to remove
the .pdf extension, wouldn't you want a regexp of
s/\.pdf$//;
rather than what you have?

Wouldn't you rather want to use File::Basename instead of a regexp?

jue
 
T

Tore Aursand

I trie to remove the extention of a file name. The folowing code work
well :

$nom_fichier_final =~ s/pdf// ;

It doesn't work well, as it doesn't necessarily remove _only_ the
filename's extension; it will remove _all_ occurances of 'pdf' in
$nom_fichier_final.

You probably want this:

$nom_fichier_final =~ s/\.pdf$//i;
my $motif = 'pdf' ;
$nom_fichier_final =~ s/$motif// ;

This should also work, so there must be a problem somewhere else in your
code.


--
Tore Aursand <[email protected]>
"Omit needless words. Vigorous writing is concise. A sentence should
contain no unnecessary words, a paragraph no unnecessary sentences,
for the same reason that a drawing should have no unnecessary lines
and a machine no unnecessary parts." (William Strunk Jr.)
 
T

Tore Aursand


Please don't top-post. It's a bad thing.
I just come to find the error : there was a blanck just after $motif and I
did nit see it ! :)

You're wrong. According to your original post:

my $motif = 'pdf' ;
$nom_fichier_final =~ s/$motif// ;

No extra space there, except one before the semi-colon (which is ugly, by
the way).

Maybe you _didn't_ copy and paste your code? Maybe you rewrote it (and,
by accident, got it right)?
 
F

fred

in fact my programm is more complicated : it removes the extension of the
file, it removes the blancs and truncate the file name. It is ugly but it
works ;-)


while (my $nom_fichier = <LISTE>) {
chomp($nom_fichier); # file name
$nom_fichier =~
m/$source_directory\/(.{5,$longueur_maxi_titre_final}).*$/ ; # truncate
the file name
my $nom_fichier_final = $1;
my $motif = '\.pdf' ;
$nom_fichier_final =~ s/$motif// ; # remove PDF
$nom_fichier_final =~ s/ /_/g ; # remove blancs
$nom_fichier_final = "$target_directory"."/$nom_fichier_final"
..".$extension"; # target file name
$nom_fichier =~ s/ /\\ /g ;
print "intitial : $nom_fichier\n";
print "final : $nom_fichier_final\n";
system ("cp $nom_fichier $nom_fichier_final"); # copy the new file

}
 
G

Glenn Jackman

fred said:
in fact my programm is more complicated : it removes the extension of the
file, it removes the blancs and truncate the file name. It is ugly but it
works ;-)


while (my $nom_fichier = <LISTE>) {
chomp($nom_fichier); # file name
$nom_fichier =~
m/$source_directory\/(.{5,$longueur_maxi_titre_final}).*$/ ; # truncate
the file name
my $nom_fichier_final = $1;
my $motif = '\.pdf' ;
$nom_fichier_final =~ s/$motif// ; # remove PDF
$nom_fichier_final =~ s/ /_/g ; # remove blancs
$nom_fichier_final = "$target_directory"."/$nom_fichier_final"
.".$extension"; # target file name
$nom_fichier =~ s/ /\\ /g ;
print "intitial : $nom_fichier\n";
print "final : $nom_fichier_final\n";
system ("cp $nom_fichier $nom_fichier_final"); # copy the new file
}


That could be written as:

# no reason to ever end a regex with ".*$", unless you're capturing it
my $re = qr{$source_directory/(.+)\.pdf$}o;

while (my $nom_fichier = <LISTE>) {
chomp $nom_fichier;
if ($nom_fichier =~ /$re/) {

# I assume $target_directory doesn't have any spaces
(my $nom_fichier_final = "$target_directory/$1.$extension")
=~ s/ /_/g;

print "initial: '$nom_fichier'\n";
print "final: '$nom_fichier_final'\n";

# don't have to worry about spaces in filenames when calling
# system with more then one argument
system 'cp', $nom_fichier, $nom_fichier_final;
}
}
 
F

fred

very elegant solution ;-)

because I want the final file name length between 5 and
$longueur_maxi_titre_final, I changed this line :

my $re = qr{$source_directory/(.{5,$longueur_maxi_titre_final}).*\.pdf$}o;
 
F

fred

very elegant solution ;-)

because I want the final file name length between 5 and
$longueur_maxi_titre_final, I changed this line :

my $re = qr{$source_directory/(.{5,$longueur_maxi_titre_final}).*\.pdf$}o;
 
G

Glenn Jackman

fred said:
because I want the final file name length between 5 and
$longueur_maxi_titre_final, I changed this line :

my $re = qr{$source_directory/(.{5,$longueur_maxi_titre_final}).*\.pdf$}o;

I assume your value 5 includes 4 characters for '.pdf'
That regex will not match "$source_directory/abc.pdf" because there
aren't 5 characters between the slash and the extension.

You might want to use smaller values:

$longueur_maxi_titre_final -= 4;
my $re = qr{$source_directory/(.{1,$longueur_maxi_titre_final}).*\.pdf$}o;

Or, you could capture a filename of any length and then truncate the
base filename if it's too long.
 
T

Tad McClellan

fred said:
$nom_fichier =~
m/$source_directory\/(.{5,$longueur_maxi_titre_final}).*$/ ; # truncate
the file name
my $nom_fichier_final = $1;


You should never use the dollar-digit variables unless you
have first ensured that the pattern match _succeeded_. [1]


die "match failed !" unless $nom_fichier =~ m/$source_directory...
my $nom_fichier_final = $1; # safe to use $1 here

$nom_fichier_final =~ s/ /_/g ; # remove blancs


The comment is misleading. "replace" is not the same as "remove".

$nom_fichier_final =~ tr/ /_/ ; # replace blancs (only faster)

$nom_fichier_final = "$target_directory"."/$nom_fichier_final"
.".$extension"; # target file name


Yuck!

$nom_fichier_final =
"$target_directory/$nom_fichier_final.$extension";


Now the path _looks like_ the path.

system ("cp $nom_fichier $nom_fichier_final"); # copy the new file


Better check the return value from system() if you care about
whether the copy worked or not...




[1] See me make this very same mistake here some years ago:
Message-ID: <[email protected]>
See Randal set me straight too. :)
 
R

Robin

fred said:
I just come to find the error : there was a blanck just after $motif and I
did nit see it ! :)

Le Wed, 21 Apr 2004 10:17:43 -0400, Paul Lalli a écrit :
[texte rapporté caché]

Those two code segments do exactly the same thing. If one did what you
wanted, so shoudl the other. Therefore, you're either not showing us
you're actual code, or there is something else wrong with your program
that you've discounted as the possible cause of your bug. Post a *short
but complete* program demonstrating the problem you're seeing.

BTW, it's "Perl", not "PERL". And also, if you really want to remove the
.pdf extension, wouldn't you want a regexp of
s/\.pdf$//;
rather than what you have?

Paul Lalli

yeah, I was just about to say... also, what if the file's name is
pdfpdf.pdf??? think a little.

it'd be more what Paul just said.
-Robin
 
R

Robin

fred said:
very elegant solution ;-)

because I want the final file name length between 5 and
$longueur_maxi_titre_final, I changed this line :

my $re =
qr{$source_directory/(.{5,$longueur_maxi_titre_final}).*\.pdf$}o;

thanks for telling us twice...hehe...
-Robin
 
J

Jürgen Exner

Robin said:
perl really should have a copy function.

Please define "copy".
- Do you mean copying a file? What's wrong with File::Copy?
- Do you mean copying a variable? What's wrong with a simple assignment?
- Do you mean copying a complex data structure with references? I think I
heard someone saying there is a module on CPAN.

jue
 
C

Chris Marshall

Tad McClellan said:
[1] See me make this very same mistake here some years ago:
Message-ID: <[email protected]>
See Randal set me straight too. :)

Apologies for the non-perl question - but how/where do I look up this
message from the ID ?

Thanks,
Chris
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top