Page 1 of 1

[REL]Script to delete undesirable commas

Posted: 2013-04-07 08:18:55
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.

Posted: 2013-04-07 09:38:44
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.

Re: [REL]Script to delete undesirable commas

Posted: 2013-04-07 11:53:28
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

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

Posted: 2013-04-08 19:26:19
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.

Posted: 2013-04-09 08:52:15
by antp
Rather line 507, no?
i.e. just after the "until", to do it only once, after exiting the loop.

Posted: 2013-04-09 19:44:17
by Nikolaus
:??: we have different computers. my computer lists "until" at line 504.
anyway, the updated script looks like I intended it. ;)

Posted: 2013-04-10 09:23:35
by antp
Indeed. I guess I was tired yesterday :??: (or not yet fully awake)