[REL]Script to delete undesirable commas

If you made a script you can offer it to the others here, or ask help to improve it. You can also report here bugs & problems with existing scripts.
Post Reply
Nikolaus
Posts: 5
Joined: 2013-04-07 08:03:48

[REL]Script to delete undesirable commas

Post by Nikolaus »

Some scripts deliver strings with double commas or with comma at the end. This script deletes these commas in all string fields.

Code: Select all

program CorrComma;
uses
  StringUtils1;
var
  MyField: string;  // contents of a database field
  MyFieldLen: integer; // length of MyField
  Position: integer; // position of a char
  i: integer; //counter
  nFields: integer; // number of fields

begin
  nFields := GetFieldCount();
  if nFields < 1 then exit;
  Position:=0;
  MyField :=' ';
  MyFieldLen := 0;
  
  for i:= 1 to nFields do
  begin
    If GetFieldType(i) = 'ftString' then
    begin
      MyField := trim(GetField(i));
      MyFieldLen := length (MyField);
      // delete comma at the end of string
      while copy(MyField,MyFieldLen,1)=',' do
      begin
        MyField := trim(copy(MyField,1,TLen-1));
        MyFieldLen := length (MyField);
      end;
      // change double comma to single comma
        // without space between commas
      while pos(',,', MyField) >0 do
      begin
        Position:=pos(',,', MyField);
        MyFieldLen := length (MyField);
        MyField := copy(MyField, 1, Position) + copy(MyField, Position+2, MyFieldLen-Position-1);
      end;
        // with space between commas
      while pos(', ,', MyField) >0 do
      begin
        Position:=pos(', ,', MyField);
        MyFieldLen := length (MyField);
        MyField := copy(MyField, 1, Position) + copy(MyField, Position+3, MyFieldLen-Position-2);
      end;
      SetField (i, MyField);
    end;
  end;
end.
soulsnake
Posts: 756
Joined: 2011-03-14 15:42:20
Location: France

Post by soulsnake »

Hi,

Thank Nikolaus.

There is just a little mistake on fields loop (first field starts to 0).
I also add some optimisations ;).

Code: Select all

program CorrComma;
var
  nFields: integer; // number of fields
  i: integer; //counter
  MyField: string;  // contents of a database field
  MyFieldLen: integer; // length of MyField

begin
  nFields := GetFieldCount();
  for i := 0 to nFields-1 do
  begin
    if GetFieldType(i) = 'ftString' then
    begin
      MyField := trim(GetField(i));
      MyFieldLen := length(MyField);
      // delete comma and space at the end of string
      while (MyFieldLen > 0) do
      begin
        if (StrGet(MyField, MyFieldLen) = ',') or
          (StrGet(MyField, MyFieldLen) = ' ') then
          MyFieldLen := MyFieldLen - 1
        else
          break;
      end;
      MyField := copy(MyField, 1, MyFieldLen);
      // change double comma to single comma
      // without space between commas
      MyField := StringReplace(MyField, ',,', ',');
      // with space between commas
      MyField := StringReplace(MyField, ', ,', ',');
      SetField (i, MyField);
    end;
  end;
end.
Soulsnake.
antp
Site Admin
Posts: 9665
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Re: [REL]Script to delete undesirable commas

Post by antp »

Nikolaus wrote:Some scripts deliver strings with double commas or with comma at the end. This script deletes these commas in all string fields.
Wouldn't it be good to also fix these scripts then? :D
Nikolaus
Posts: 5
Joined: 2013-04-07 08:03:48

[REL]Script "OFDb - IMDb - TMDb - mod"

Post by Nikolaus »

The Scipt "OFDb - IMDb - TMDb - mod" fills the database field "producer" with double commas as separator.

You can fix it by inserting at line 505:
Value := StringReplace(Value, ',,', ',');
Value := StringReplace(Value, ', ,', ',');
(Thx to soulsnake)

Note: This is a patch only to change the result. I'm not enough familiar with Pascal to find out the reason for the double comma separators.
antp
Site Admin
Posts: 9665
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Rather line 507, no?
i.e. just after the "until", to do it only once, after exiting the loop.
Nikolaus
Posts: 5
Joined: 2013-04-07 08:03:48

Post by Nikolaus »

:??: we have different computers. my computer lists "until" at line 504.
anyway, the updated script looks like I intended it. ;)
antp
Site Admin
Posts: 9665
Joined: 2002-05-30 10:13:07
Location: Brussels
Contact:

Post by antp »

Indeed. I guess I was tired yesterday :??: (or not yet fully awake)
Post Reply