Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

Beatnik (493)

Beatnik
  (email not shown publicly)
http://www.ldl48.org/

A 29 year old belgian who likes Mountain Dew, Girl Scout Cookies, Tim Hortons French Vanilla Flavoured Cappucinno, Belgian beer, Belgian chocolate, Belgian women, Magners Cider, chocolate chipped cookies and Perl. Likes snowboarding, snorkling, sailing and silence. Bach can really cheer him up! He still misses his dog.

Project Daddy of Spine [sf.net], a mod_perl based CMS.

In his superhero time (8.30 AM to 5.30 PM), he works on world peace.

Journal of Beatnik (493)

Wednesday October 15, 2003
04:56 AM

Delphi vs Perl #2

[ #15217 ]
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!!
The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • Free libs... (Score:2, Informative)

    FYI, doing a quick google, i found a free regex lib for Delphi, here [latiumsoftware.com].

    *shrug*

  • 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.
    • 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.
    • 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.
      • 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 :)