Delphi vs Perl #2

Beatnik on 2003-10-15T09:56:04

One of the questions any programmers asks himself now and then... How to delete trailing spaces from a string?

In Delphi, you're forced to do something like:

procedure RightTrim32(var Str: String);
var
SPos,SLen: Integer;
begin
Spos := Length(Str);
if Str[SPos] = ' ' then
begin
SLen := 0;
while (SPos > 0) and (Str[SPos] = ' ')
do
begin
Inc(SLen);
Dec(SPos)
end;
Delete(Str,SPos+1,SLen);
end
end;


In Perl, as we all know, it's MUCH shorter.

$string =~ s/ +$//;

Thank you, Larry!!


Free libs...

LTjake on 2003-10-15T11:39:03

FYI, doing a quick google, i found a free regex lib for Delphi, here.

*shrug*

Bug?

bart on 2003-10-18T01:20:56

I know nothing of Delphi, but it looks to me like this might be a bug. At least, if array indexes start at zero by default 'according to the little lookups on the web I did, tht is the case), and you don't insist on keeping the first character no matter what.
while (SPos > 0) and (Str[SPos] = ' ')
I think the ">" must be ">=". Otherwise, you'd never test, or remove, the very first character.

Re:Bug?

Beatnik on 2003-10-18T11:02:48

Well actually, that snippet came from Bob Swart aka Dr. Bob. I eventually went for a routine that deleted everything after the first occurance of a space. Not really exactly what I needed but it'll do for the data I'm using it on.

Re:Bug?

LeFox on 2004-07-27T14:54:29

Couldn't help replying this one.
But whats wrong with the Delphi implemented TrimRight() function:

> MyString TrimRight(MyString)

Of course I admit that Perl is better at Text processing. But, opposed to Perl, Delphi intended as a Language to make the creation of Windows, event driven GUI's easy.
Not to make it easy to transform one bunch of ASCII-characters into another bunch of ASCII-characters.

Re:Bug?

Beatnik on 2004-07-27T16:08:54

When using another programming language, I constantly get the "This is MUCH easier in perl" feeling. I'm not quite sure why but it even happens with languages I know... Delphi is one of them. I'm lucky I got out of the project alive :)