Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Re^4: How to remove underscore at the end of the line?

by finddata (Sexton)
on Mar 17, 2017 at 06:21 UTC ( #1184965=note: print w/replies, xml ) Need Help??


in reply to Re^3: How to remove underscore at the end of the line?
in thread How to remove underscore at the end of the line?

sub ir($$); sub ir($$) { my $prefix = shift @_; $prefix=~ s/_$//; print $prefix,"\n"; my $dir = shift @_; print "&&&$dir&&&","\n"; opendir(DIR, $dir) or die $!; my @entries = readdir(DIR); print "****@entries***"; close(DIR); foreach my $file (@entries) { next if ($file =~ /^\.+$/); if ( -d $dir . '/' . $file) { ir($prefix . $file .'_', $dir . '/' . $file); } elsif ( ( -f $dir . '/' . $file ) && ( $file =~ /\.config$/ +) && ($file !~ /^$prefix/)) { my $suffix = $file; $suffix=~s{\A[^.]*}{}xms; print "^^^$suffix^^^","\n"; # print $suffix,"\n"; rename $dir . '/' . $file, $dir . '/' . $prefix . $suffix +; } } } ir('',$output_dir); here print prefix has underscore at the end of the line

Replies are listed 'Best First'.
Re^5: How to remove underscore at the end of the line?
by davido (Cardinal) on Mar 17, 2017 at 06:42 UTC

    Thank you for posting some code. It's not a great code example because it contains code that is not part of the problem, doesn't compile, and doesn't demonstrate how it's being used with some sample data. But it's enough to test your assertion.

    The portion of what you posted that is relevant to your assertion that the code snippet provided in this thread doesn't work is the first three lines of the subroutine ir().

    # First three lines of your subroutine, ir() with the prototype remove +d because it's not useful: sub ir { my $prefix = shift @_; $prefix=~ s/_$//; print $prefix,"\n"; } # A loop to test those first three lines of your code to see if they w +ork: while(<DATA>) { chomp; ir($_); } # Sample input you provided: __DATA__ DCMS_DEMO_ DCMS_DEMO_new_block2_ DCMS_DEMO_new_block2_checklist_tmp_ DCMS_DEMO_new_block2_checklist_tmp_rev1_

    ....output....

    DCMS_DEMO DCMS_DEMO_new_block2 DCMS_DEMO_new_block2_checklist_tmp DCMS_DEMO_new_block2_checklist_tmp_rev1

    It works.


    Dave

Re^5: How to remove underscore at the end of the line?
by huck (Parson) on Mar 17, 2017 at 06:52 UTC

    It is real hard to believe that when passed the empty string as prefix it would have an underscore at the end of it ya know, your example leaves a whole lot to be desired

    This $prefix=~ s/_$//;takes one underscore off the end. To take all underscores off the end you would need $prefix=~ s/_$//g; or $prefix=~ s/_+$//;. But that wasnt what you asked was it? you should have asked how to remove ALL underscores from the end.

    Now why might there be more than one underscore at the end of $prefix? well dont you add one when you say ir($prefix . $file .'_', $dir . '/' . $file); and if $file already had one at the end you just made it two didnt you?

    so if you dont want an underscore at the end of $prefix then just why do you add one when you call ir()? i would love to hear that explanation.....

    another explanation i would like to hear is what good the $$ prototype is doing for you, can you explain why you need it?

    and what is going to happen if you ever hit a file with TWO dots in it?

      Our friend has a problem saying what he means. It seems that the "input" is actually a directory structure containing the path DCMS_DEMO/new_block2/checklist_tmp/rev1, and the "output" is what some file is getting renamed to (not what's printed by any of the random debugging prints). Also, he's run 57 different varieties of his script and he's confused about which runs produced which results.

        i know, just kinda saying "gold knight gray sea"!

Re^5: How to remove underscore at the end of the line?
by Anonymous Monk on Mar 17, 2017 at 06:39 UTC

    Hi,

    Sorry, that doesn't compile

    $ perl finddata syntax error at finddata line 30, near "here print" Execution of finddata aborted due to compilation errors.

    Try again please, there is an old tutorial to help you with that

Re^5: How to remove underscore at the end of the line?
by Anonymous Monk on Mar 17, 2017 at 06:51 UTC
    Instead of $prefix =~ s/_$//;, maybe try this:
    my $newfile = $prefix . $suffix; $newfile =~ s/_$//; rename "$dir/$file", "$dir/$newfile";

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (3)
As of 2020-01-24 01:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?