For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > April 2004 > variable interpolation failed :-(









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author variable interpolation failed :-(
fred

2004-04-21, 10:52 am

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 !
Paul Lalli

2004-04-21, 10:52 am

On Wed, 21 Apr 2004, fred wrote:

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

2004-04-21, 11:43 am

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 :

> On Wed, 21 Apr 2004, fred wrote:
>
>
> 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ürgen Exner

2004-04-21, 11:43 am

Paul Lalli wrote:
> On Wed, 21 Apr 2004, fred wrote:
[...]
And also, if you really want to remove[color=darkred]
> 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


Tore Aursand

2004-04-21, 11:43 am

On Wed, 21 Apr 2004 16:09:16 +0200, fred wrote:
> 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 <tore@aursand.no>
"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.)
Tore Aursand

2004-04-21, 12:53 pm

On Wed, 21 Apr 2004 16:31:00 +0200, fred wrote:
> [...]


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)?


--
Tore Aursand <tore@aursand.no>
"Time only seems to matter when it's running out." (Peter Strup)
fred

2004-04-21, 2:45 pm

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

}



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


Glenn Jackman

2004-04-21, 4:38 pm

fred <fred@no-spam.fr> wrote:
> 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;
}
}


--
Glenn Jackman
NCF Symin
glennj@ncf.ca
fred

2004-04-21, 5:37 pm

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;


fred

2004-04-21, 5:37 pm

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

2004-04-21, 6:37 pm

fred <fred@no-spam.fr> wrote:
> 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.

--
Glenn Jackman
NCF Symin
glennj@ncf.ca
Tad McClellan

2004-04-21, 6:37 pm

fred <fred@no-spam.fr> wrote:

> $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: <uk68f9cg8b.fsf@linda.teleport.com>
See Randal set me straight too. :-)

--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Tad McClellan

2004-04-22, 2:35 am

fred <fred@no-spam.fr> wrote:

> very elegant solution ;-)



What is?


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Robin

2004-04-25, 8:34 pm


"fred" <fred@no-spam.fr> wrote in message
news:pan.2004.04.21.14.30.57.770891@no-spam.fr...[color=darkred]
> 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 :
>
the[color=darkred]

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


Robin

2004-04-25, 8:34 pm


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


perl really should have a copy function.

--
Regards,
-Robin
--
[ webmaster @ infusedlight.net ]
www.infusedlight.net


Robin

2004-04-25, 8:34 pm


"fred" <fred@no-spam.fr> wrote in message
news:pan.2004.04.21.20.51.50.13080@no-spam.fr...
> 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ürgen Exner

2004-04-25, 8:34 pm

Robin wrote:
>
> 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


Tad McClellan

2004-04-25, 10:52 pm

Robin <robin@infusedlight.net> wrote:
>


Who said that?

Please provide an attribution when you quote someone.

[color=darkred]
> perl really should have a copy function.



It does, File::Copy.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Tad McClellan

2004-04-27, 1:51 am

Robin <robin@infusedlight.net> wrote:
>


Who said that?

Please provide an attribution when you quote someone.

[color=darkred]
> perl really should have a copy function.



It does, File::Copy.


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Chris Marshall

2004-04-27, 2:05 am

Tad McClellan <tadmc@augustmail.com> wrote in message
> [1] See me make this very same mistake here some years ago:
> Message-ID: <uk68f9cg8b.fsf@linda.teleport.com>
> 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
Tad McClellan

2004-04-27, 2:05 am

Chris Marshall <c_j_marshall@hotmail.com> wrote:
> Tad McClellan <tadmc@augustmail.com> wrote in message
>
> Apologies for the non-perl question - but how/where do I look up this
> message from the ID ?



You've been missing the 2nd best (after the std docs) Perl
resource of them all!


http://groups.google.com/advanced_group_search


--
Tad McClellan SGML consulting
tadmc@augustmail.com Perl programming
Fort Worth, Texas
Chris Marshall

2004-04-27, 2:09 am

Tad McClellan <tadmc@augustmail.com> wrote in message news:<slrnc8q0c5.ovs.tadmc@magna.augustmail.com>...
> Chris Marshall <c_j_marshall@hotmail.com> wrote:
>
>
> You've been missing the 2nd best (after the std docs) Perl
> resource of them all!
>
>
> http://groups.google.com/advanced_group_search



ah - thanks.
I already use google for searching old perl questions in this
newsgroup - but I'd never spotted the Message ID field in the advanced
search.

Thanks again.
Chris
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com