variable interpolation failed :-(

Discussion in 'Perl Misc' started by fred, Apr 21, 2004.

  1. fred

    fred Guest

    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 !
     
    fred, Apr 21, 2004
    #1
    1. Advertising

  2. fred

    Paul Lalli Guest

    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
     
    Paul Lalli, Apr 21, 2004
    #2
    1. Advertising

  3. fred

    fred Guest

    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:
    >
    >> [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
     
    fred, Apr 21, 2004
    #3
  4. Paul Lalli wrote:
    > On Wed, 21 Apr 2004, fred wrote:
    >> I trie to remove the extention of a file name.

    [...]
    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
     
    Jürgen Exner, Apr 21, 2004
    #4
  5. fred

    Tore Aursand Guest

    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 <>
    "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, Apr 21, 2004
    #5
  6. fred

    Tore Aursand Guest

    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 <>
    "Time only seems to matter when it's running out." (Peter Strup)
     
    Tore Aursand, Apr 21, 2004
    #6
  7. fred

    fred Guest

    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
     
    fred, Apr 21, 2004
    #7
  8. fred <> 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 Sysadmin
     
    Glenn Jackman, Apr 21, 2004
    #8
  9. fred

    fred Guest

    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, Apr 21, 2004
    #9
  10. fred

    fred Guest

    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, Apr 21, 2004
    #10
  11. fred <> 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 Sysadmin
     
    Glenn Jackman, Apr 21, 2004
    #11
  12. fred <> 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: <>
    See Randal set me straight too. :)

    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 21, 2004
    #12
  13. fred <> wrote:

    > very elegant solution ;-)



    What is?


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 22, 2004
    #13
  14. fred

    Robin Guest

    "fred" <> wrote in message
    news:p...
    > 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:
    > >
    > >> [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
     
    Robin, Apr 25, 2004
    #14
  15. fred

    Robin Guest

    "fred" <> wrote in message
    news:p...
    > 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
     
    Robin, Apr 26, 2004
    #15
  16. fred

    Robin Guest


    >
    > 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, Apr 26, 2004
    #16
  17. Robin wrote:
    >> Better check the return value from system() if you care about
    >> whether the copy worked or not...
    >>

    >
    > 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
     
    Jürgen Exner, Apr 26, 2004
    #17
  18. Robin <> wrote:
    >
    >>
    >> Better check the return value from system() if you care about
    >> whether the copy worked or not...



    Who said that?

    Please provide an attribution when you quote someone.


    > perl really should have a copy function.



    It does, File::Copy.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 26, 2004
    #18
  19. Tad McClellan <> wrote in message
    > [1] See me make this very same mistake here some years ago:
    > Message-ID: <>
    > 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
     
    Chris Marshall, Apr 26, 2004
    #19
  20. Chris Marshall <> wrote:
    > Tad McClellan <> wrote in message
    >> [1] See me make this very same mistake here some years ago:
    >> Message-ID: <>
    >> See Randal set me straight too. :)

    >
    > 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
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Apr 26, 2004
    #20
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. \Rob\
    Replies:
    8
    Views:
    646
    Daniel Kirsch
    Feb 24, 2006
  2. Andrew Fabbro

    Variable interpolation question

    Andrew Fabbro, Nov 17, 2003, in forum: Python
    Replies:
    3
    Views:
    292
    anton muhin
    Nov 17, 2003
  3. Oliver Heins
    Replies:
    2
    Views:
    122
    Oliver Heins
    Sep 11, 2006
  4. Praveen Kallakuri
    Replies:
    3
    Views:
    164
    Praveen Kallakuri
    Apr 15, 2004
  5. phrankster

    Variable Interpolation with %%variable

    phrankster, Aug 9, 2005, in forum: Perl Misc
    Replies:
    9
    Views:
    147
    James Taylor
    Aug 14, 2005
Loading...

Share This Page