Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Removing dots

by Anonymous Monk
on May 08, 2005 at 13:49 UTC ( #454999=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

I would like to remove the dots from Hello.1.txt ie Hello1.txt. How do i do so?

Comment on Removing dots
Replies are listed 'Best First'.
Re: Removing dots
by tlm (Prior) on May 08, 2005 at 14:06 UTC

    It's hard to say what the best solution would be without knowing exactly what dots you don't want removed, but for the example you give,

    s/\.//;
    does the job. Or
    s/\.(?!txt$)//g
    ...or
    s/\.(?![^.]+$)//g

    the lowliest monk

      ok let's say if i want it to look like Hello.1txt would s/\.(?=txt$)//g do the job?

        You can get your answer more quickly by firing up the debugger and just trying it out:

        % perl -de 1 DB<1> $s = 'Hello.1.txt' DB<2> $s =~ s/\.(?=txt$)//g # in this case the /g is superfluous DB<3> p $s Hello.1txt
        And faster still (in the long run) if you just study perlre.

        the lowliest monk

        If you really want to remove all but the first occurance:

        $filename =~ /\./; substr($filename, $-[0]+1) =~ s/\.//g if @-;

Re: Removing dots
by Dietz (Curate) on May 08, 2005 at 14:11 UTC
    This would remove all dots except the rightmost one:
    s/\.(?=.*\.)//g;

    A single dot occuring only once in the string will not be removed.
Re: Removing dots
by davidrw (Prior) on May 08, 2005 at 14:47 UTC
    If it's just the base name you need to manipulate (i.e. leave the '.txt. intact), can take advantage of File::Basename to parse that section for you, then do what you want with it. That way the regex is simple because it doesn't have to deal w/the exception of the extension.
    # given: $fullname, @suffixlist (see pod for latter) use File::Basename; my ($name,$path,$suffix) = fileparse($fullname,@suffixlist); $name =~ s/\.//g; my $new_fullname = $path . $name . $suffix;
Re: Removing dots
by ambrus (Abbot) on May 08, 2005 at 16:09 UTC
Re: Removing dots
by Angharad (Pilgrim) on May 08, 2005 at 14:53 UTC
    How about using a combination of substring and then cat the resulting variables together?
Re: Removing dots
by mattk (Pilgrim) on May 08, 2005 at 23:15 UTC
    Just because I felt like doing something different than an RE:
    $origfile = shift; @chunks = split(/\./, $origfile); $ext = pop @chunks; $filename = join("", @chunks, ".$ext"); rename $origfile, $filename;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (5)
As of 2015-07-31 03:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls