For Programmers: Free Programming Magazines  


Home > Archive > PERL Beginners > March 2005 > How to remove everything after the last "." dot?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author How to remove everything after the last "." dot?
Bastian Angerstein

2005-03-08, 8:56 am


I have a line:
i.like.donuts.but.only.with.tea

now I want to remove everything that follows the last "."
including the last ".".

in this case ".tea"

I hav absulte no idea. I tried something like:

@parts = splite /\./, $line;

$parts[-1] = undef;

foreach $parts (@parts) {
$newline .= "$parts.";
}

but then I got a dot at the end of the line.

Thanks,

Bastian
John Doe

2005-03-08, 8:56 am

Hi

> I have a line:
> i.like.donuts.but.only.with.tea
>
> now I want to remove everything that follows the last "."
> including the last ".".
>

[...]

you can use a regex for that.

=== Documentation (from command line:)


perldoc perlre

=== Code:

use strict; use warnings;
my $a="i.like.donuts.but.only.with.tea";
$a=~s/\.\d+$//; # <---- this does the trick
print $a, "\n";

# this prints:
i.like.donuts.but.only.with.tea

=== explanation:
see documentation ;-)


greetings joe
Gretar Mar Hreggvidsson

2005-03-08, 8:56 am

Hi!

There is actually slight error in this code.

> $a=~s/\.\d+$//; # <---- this does the trick


This handles only one or more digits (\d stand for digits), so unless
the last part of the string is a number this won't work.
Should be something like

$a=~s/\.\w+$//; # (\w stands for word charachters)

Gretar Mar

John Doe wrote:
> Hi
>
>
>
> [...]
>
> you can use a regex for that.
>
> === Documentation (from command line:)
>
>
> perldoc perlre
>
> === Code:
>
> use strict; use warnings;
> my $a="i.like.donuts.but.only.with.tea";
> $a=~s/\.\d+$//; # <---- this does the trick
> print $a, "\n";
>
> # this prints:
> i.like.donuts.but.only.with.tea
>
> === explanation:
> see documentation ;-)
>
>
> greetings joe

Ramprasad A Padmanabhan

2005-03-08, 8:56 am

Bastian Angerstein wrote:
> I have a line:
> i.like.donuts.but.only.with.tea
>
> now I want to remove everything that follows the last "."
> including the last ".".
>
> in this case ".tea"
>


Use Regex

To read the manual
perldoc perlre

Of course In your case you could simply do something like this
$line =~s/\.[^.]+$//;


Thanks
Ram
John W. Krahn

2005-03-09, 3:56 am

Bastian Angerstein wrote:
> I have a line:
> i.like.donuts.but.only.with.tea
>
> now I want to remove everything that follows the last "."
> including the last ".".
>
> in this case ".tea"


substr( $line, rindex( $line, '.' ) ) = '' if $line =~ tr/.//;


John
--
use Perl;
program
fulfillment
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2009 codecomments.com