Measurement converter in Pascal lazarus / 67-Windows ---------------------------------------------------------------------- dzoole · 2022-03-16 16:51 Hi, i' m a newbie in pascal programing. I have need for measurement converter in Pascal to use in Subtitle Workshop as pascal script. Found some codes online for other converters, but can't figure out how to do it for example miles to kilometres. Can someone help me to do it. This is the pascal script. unit units; interface uses   Classes,   Controls,   Dialogs,   Forms,   StrUtils,   SysUtils,   Variants,   Windows; begin function MilesToKillometres(MyNumber: variant): string; function InchesToCentimetres(MyNumber: variant): string; function FeetToCentrimetres(MyTens: variant): string; function GalonsToLitres(MyDigit: variant): string; implementation function MilesToKillometres(MyNumber: variant): string; var Temp: string;   Miles, Killometres, Words: string;   km, iCount: string; begin   //Find any number before miles   Words:= (MyNumber + 'miles'); end;   begin     //Convert Miles     Temp  := LeftStr((MyNumber + 'miles') * 1,6);     km := ' and ' + ConvertMiles(Temp) + ' km';   end; end. ---------------------------------------------------------------------- winni · 2022-03-16 17:12 Hi! A simple solution which exstracts the number from a string, coverts miles to km an gives a output: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i function MilesToKm (miles: string) : string;var i : integer;    mileVal, kmVal : single;    tmp : string;begin  tmp := '';  for i := 1 to length(miles) do      if miles[i] in ['0'..'9','.'] then tmp := tmp+miles[i];  mileVal := strToFloat(tmp);  kmVal := 1.609344 * mileVal;  // kmVal := 1.852 * mileVal; ==>nautic mile  result := FloatToStr(kmVal)+' Kilometer';  end;           The decimal separator in Pascal is a dot! Winni ---------------------------------------------------------------------- marcov · 2022-03-16 17:13 In Free pascal/Lazarus, the unit with measurement conversions is convutils, and is modelled after the same unit in Delphi (though not 100% compatible yet) Winni: strtofloat is locale dependent (using either comma or dot as decimal separator), while your set check isn't. ---------------------------------------------------------------------- dzoole · 2022-03-16 17:36 Thank you for your fast answer. And yes in my system locale , coma is decimal separator. I'll try the script now, to see does it work in Subtitle Workshop. I don't why but I can't compile it. When I run Subtitle Workshop says error compiler expected closed bracket ] In Lazarus syntax check OK, but can't compile. ---------------------------------------------------------------------- winni · 2022-03-16 17:57 Hi! For Lazarus error: show us the error message. We have no crystall balls. For Subtitle Workshop: Ask there. This is a Lazarus/fpc Forum. Winni ---------------------------------------------------------------------- dzoole · 2022-03-16 18:35 Like I said i'm newbie at this, so it's probably my mistake. ---------------------------------------------------------------------- VTwin · 2022-03-16 18:59 "result" is the return of a function. Make a function to call in your program. "miles" is not defined. A program is terminated with "." ---------------------------------------------------------------------- Josh · 2022-03-16 19:27 hi dzoole you have messed up winni code; if you look it is not the same.. you have implementation section in the function code. the function code should be seperate to the implementation interface etc Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  // declares these functions/procedures as they could be used before they have been defined// ie if you had a function/procedure that called another one that was further down your program code.// you must only add the function/procedure definition; it must match exactly the actual function/procedure definition// and have no actual code in it. Function MilesToKm (miles: string) : string; implementation Function MilesToKm (miles: string) : string;var i : integer;    mileVal, kmVal : single;    tmp : string;begin  tmp := '';  for i := 1 to length(miles) do      if miles[i] in ['0'..'9','.'] then tmp := tmp+miles[i];  mileVal := strToFloat(tmp);  kmVal := 1.609344 * mileVal;  // kmVal := 1.852 * mileVal; ==>nautic mile  result := FloatToStr(kmVal)+' Kilometer';end; // all other functions here  {$R *.lfm}   can you see the difference ---------------------------------------------------------------------- dzoole · 2022-03-16 20:01 Like i said its probably my mistake. Now, in which file i should paste wiini's code? In Lazarus ->File->New-> and then what? Pascal Unit? In Subtitle Workshop, software for subtittles where I want to use the pascal script, i get this. ---------------------------------------------------------------------- dzoole · 2022-03-16 20:33 According to this, i'm not on the right path. https://docwiki.embarcadero.com/Libraries/Sydney/en/System.ConvUtils ---------------------------------------------------------------------- marcov · 2022-03-16 21:04 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program doconvertmilestokm;{$mode delphi} uses  ConvUtils,stdconvs,sysutils;   var  miles,kilometers : Double; begin  // Define the miles value  miles := 1;   // Convert to km  kilometers := Convert(miles, duMiles, duKilometers);   // Display both values  writeln(format('%f miles  = %f kilometers',[miles,kilometers ]));end. ---------------------------------------------------------------------- dzoole · 2022-03-16 21:26 Now says coudn't identify ConvUtils. Never mind, thanks anyway for your time. ---------------------------------------------------------------------- af0815 · 2022-03-17 06:14 Quote from: dzoole on March 16, 2022, 10:26:14 pmNow says coudn't identify ConvUtils. You are not using fpc/Lazarus. Are you using Subtitle workshop with pascalscript ? In Lazarus you have to make a new program with 'Project->New Project->Simple Program' to create a new programm and insert the sample code. ---------------------------------------------------------------------- dbannon · 2022-03-17 06:49 dz, if all you want to do is convert miles to kilometers, just multiply by 1.609, does not need any special units at all. Its a simple floating point calc, ability to do that is there already. Do you really need a Pascal Script ?  Or just a binary called, eg miles2km ? A line of code to check you have a just a number on the command line, a line of code to multiply it by 1.609, write that to std out. Davo ---------------------------------------------------------------------- dzoole · 2022-03-17 10:00 I'm not using Subtitle Workshop to compile. I'm using it for subtitles, but if there is some problem with pascal scripts, subtitle workshop gives an info. Subtitle Workshop is write in Delphi and can use pascal scripts for bunch of things with subtitles. Most of them are for timings, FPS, etc... So the idea was this. I don't know how to write those in Pascal. Find any number before word miles and than multiple that number to 1.6 and replace word miles with kilometres. And the idea was to add this type of conversion for feet to meters, inches to centimetres etc, etc.. Too bad that i can't multiple in reg ex In reg ex i can only mark "miles" and convert into text "x1.6 kilometres" which will be reminder to turn on calculator and calculate. Find some pascal script for conversions without System.ConvUnits. In the attachment I don't know how to adapt for my purpose. ---------------------------------------------------------------------- Zvoni · 2022-03-17 10:44 Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i program Project1;Uses SysUtils;Const  SomeText:String='I am running 4.5 miles every day';  mTok:Double=1.609; Var  a:TStringArray;  i:Integer;  d:Double;  s:String;  fs:TFormatSettings; begin  fs:=DefaultFormatSettings;  a:=SomeText.Split(' ');  For i:=Low(a) To High(a) Do    Begin      If a[i].Trim='miles' Then        Begin        If Not TryStrToFloat(a[i-1],d,fs) Then            If fs.DecimalSeparator='.' Then              fs.DecimalSeparator:=','          Else              fs.DecimalSeparator:='.';          a[i-1]:=FloatToStr((StrToFloat(a[i-1],fs)*mTok),fs);          a[i]:='kilometers';        end;    end;  s:=s.Join(' ',a);  Writeln(s);end.    Returns: QuoteI am running 7.2405 kilometers every day ---------------------------------------------------------------------- dzoole · 2022-03-17 11:12 Zvoni your code seems fine in Lazarus Syntax check OK. When I compile get succes but 1 warning. I saved your file as .pas Copy to Pascal Scripts folder in Subtitle Workshop and when i want to use it i get Sys]"]>Blockedtils not identified. This Subtitle Workshop is very tricky with pascal scripts. Some pascal scripts works fine in 2.51 version but not good in other versions. 6.0a and 6.0b/e