Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

renaming the file using perl

by finddata (Sexton)
on Mar 16, 2017 at 04:53 UTC ( #1184818=perlquestion: print w/replies, xml ) Need Help??

finddata has asked for the wisdom of the Perl Monks concerning the following question:

foreach my $file (@entries) { next if ($file =~ /^\.+$/); if ( -d $dir . '/' . $file) { runDir($prefix . $file . '_', $dir . '/' . $file); print "***up runDir($prefix . $file . '_', $dir . '/' . + $file)\n"; } elsif ( ( -f $dir . '/' . $file ) && ( $file =~ /\.config$/ +) && ($file !~ /^$prefix/)) { my $suffix = $file; $suffix =~ s/^(\S+)-.+\.config$/$1.config/; print "****rename $dir . '/' . $file, $dir . '/' . $prefix . $su +ffix"."\n"; rename $dir . '/' . $file, $dir . '/' . $prefix . $suffix +; } }
Expected output after rename:
DCMS_DEMO_new_block2_checklist_tmp_rev2.html
Now the output occurs as follows:
prefix output: ***up runDir(DCMS_DEMO_de_top_Block_DV_ . rev2 . '_', /home/sa/dcms_ +html_output/DCMS_DEMO/de_top/Block_DV . '/' . rev2) suffix output: Block_DV.config rename output: ****rename /home/sa/dcms_html_output/DCMS_DEMO/de_top/Block_DV . '/' . Block_DV.c +onfig, /home/sa/dcms_html_output/DCMS_DEMO/de_top/Block_DV . '/' . DC +MS_DEMO_de_top_Block_DV_ . Block_DV.config

Replies are listed 'Best First'.
Re: renaming the file using perl
by nysus (Vicar) on Mar 16, 2017 at 05:28 UTC

    If I'm guessing your intentions correctly, you have some basic problems with your print statments. First, it looks like your print statements are trying to concatenate strings inside the quotation marks. This won't work. Your print statement should be something like this:

    print "***rename $dir/$file", "$dir/$prefix$suffix\n"

    $PM = "Perl Monk's";
    $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
    $nysus = $PM . ' ' . $MCF;
    Click here if you love Perl Monks

      by using the line which provided i got output as follows: rename /home/sa/dcms_html_output/DCMS_DEMO/de_top/Block_DV/Block_DV.co +nfig, /home/sa/dcms_html_output/DCMS_DEMO/de_top/Block_DV/DCMS_DEMO_d +e_top_Block_DV_1
      expected should be as follows:
      /home/sa/dcms_html_output/DCMS_DEMO/de_top/Block_DV/Block_DV.config, / +home/sa/dcms_html_output/DCMS_DEMO/de_top/Block_DV/DCMS_DEMO_de_top_B +lock_DV.config

        Your code is very hard to follow. Can I suggest you please:

        • Place comments in your code to help explain what you are trying to accomplish and what you think each chunk of code is doing
        • Use variable names in place of your long string concatenations to make it more readable
        • Use in-line comment for each variable name to help explain what the variable represents

        This will not only help us help you, it will help you understand your own code better. When you are new at programming, it's very difficult to keep track of what you are trying to do and it is easy to get lost. Comments and liberal use of variables will help you out a great deal.

        $PM = "Perl Monk's";
        $MCF = "Most Clueless Friar Abbot Bishop Pontiff Deacon Curate";
        $nysus = $PM . ' ' . $MCF;
        Click here if you love Perl Monks

Re: renaming the file using perl
by madtoperl (Friar) on Mar 16, 2017 at 10:28 UTC
    Hi finddata
    Please narrow down your question by showing what exactly is giving you trouble. Looks like you are making it more complicated to rename a file.
    Maybe you are trying to generate a file based on the input from some existing data. Eg. If data is having configuration related data, then generate the output file with suffix as .config file If data is having html related data, then generate the output file with suffix as .html

    If that is the case, you can do it in more easier way than doing this renaming stuff like adding the sufix based on the input data.
Re: renaming the file using perl
by Anonymous Monk on Mar 16, 2017 at 05:29 UTC

    Hi,

    What is your question?

    A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1184818]
Approved by beech
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2020-01-21 04:50 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?