Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Substitution or removing characters from a file

by hary536 (Novice)
on May 10, 2012 at 02:57 UTC ( #969729=perlquestion: print w/ replies, xml ) Need Help??
hary536 has asked for the wisdom of the Perl Monks concerning the following question:

Hello Friends, This is my first post and I just recently started coding in Perl.

I am trying to remove some characters from a file in a pattern described below. I have accomplished the goal, but it takes two lines of code. I am wondering, if this can be reduced to a single line.

I have a file with a single line in it as follows: /Users/name/Desktop/Reprocess_120509_171210/Name1/Name2/RDLy_XXXX_2012_03_25_1850.ruv I am trying to modify this line so that I have only RDLy_XXXX_2012_03_25_1850 remaining, which I am then going to use as a name for a picture file by appending .png to the above name. Here is what I did:
open (FIDR, "< $radFile"); chomp($basename = <FIDR>); $basename =~ s/\S*RDL/RDL/; ## this removes everything before RDL but +has ".ruv"extension $basename =~ s/.ruv$//; ##this removes the .ruv extension.
I am wondering, can the above substitution task(of 2 lines) be accomplished in a single line? If yes, how? Thanks a lot in advance.

Comment on Substitution or removing characters from a file
Download Code
Re: Substitution or removing characters from a file
by NetWallah (Abbot) on May 10, 2012 at 03:19 UTC
    Try this:
    my ($NameWithoutExtension)=$basename=~/(RDL.+)\.\w+/;

                 I hope life isn't a big joke, because I don't get it.
                       -SNL

      Hello, Thanks for the reply. Am late in acknowledging it, was on travel. Thanks, it worked.
Re: Substitution or removing characters from a file
by jwkrahn (Monsignor) on May 10, 2012 at 04:02 UTC
    open my $FIDR, '<', $radFile or die "Cannot open '$radFile' because: $ +!"; my ( $basename ) = <$FIDR> =~ m{/([^/]+)\.ruv$};

    Update: fixed filehandle.

      Hello, Thanks for the reply. Am late in acknowledging it, was on travel. I will try it out and let you know, how it went. Thanks.
Re: Substitution or removing characters from a file
by JavaFan (Canon) on May 10, 2012 at 06:37 UTC
    Sure, just remove the newline:
    $basename =~ s/\S*RDL/RDL/; $basename =~ s/.ruv$//;
    Or
    s/\S*RDL/RDL/, s/.ruv$// for $basename;
    Or
    $basename =~ s/\S*RDL((?s:.*)).ruv$/RDL$1/;
    but why would you? The two lines are easy to understand, and certainly more efficient that the latter single substitution.
      Hello, Thanks for the reply. Am late in acknowledging it, was on travel. Thanks for your above suggestions. You said that two lines are more efficient than the single substitution. How can I tell, which part of my code is more efficient than other. I thought that 1 line would be more efficient than 2 lines, hence I posted this question. You have any suggestion/reading link for reading on writing efficient code in perl? Thanks.
        It was me who posted above msg. Not sure, how I got logged out in the process before posting that msg.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (6)
As of 2014-08-28 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (253 votes), past polls