Re: [fpc-pascal] Get highest element of a StringList
But, if that (eventually) leads to the highest one, and that's all that's desired, why not just use the whole expression variable := pred(f.Count) instead of the whole loop? Wouldn't that accomplish the same thing? Why the loop? On 9/10/2022 3:44 PM, Thomas Kurz via fpc-pascal wrote: If you just don't like the "-1" for readability, you might also wan't to consider using for i := 0 to Pred(f.Count) do ... - Original Message - From: Thomas Kurz via fpc-pascal To: 'FPC-Pascal users discussions' Sent: Saturday, September 10, 2022, 21:37:34 Subject: [fpc-pascal] Get highest element of a StringList Try this (note the "modeswitch"): program Project1; {$modeswitch typehelpers} uses Classes, SysUtils; type TStringListHelper = class helper for TStringList function High: NativeInt; end; function TStringListHelper.High: NativeInt; begin Exit (Self.Count-1); end; var f: TStringList = nil; i: Integer; begin f := TStringList.Create; f.Add ('hallo'); f.Add ('welt'); f.Add ('!'); for i := 0 to f.High do Writeln (f[i]); FreeAndNil (f); end. - Original Message - From: James Richters via fpc-pascal To: 'FPC-Pascal users discussions' Sent: Saturday, September 10, 2022, 19:57:51 Subject: [fpc-pascal] Get highest element of a StringList This Helper sounds like a nice way to do it. I need the numerical index of the loop so this would be more straight forward. Would I then do something like : For I:= 0 to MyStringList.High Do ? When I try to add the Type statement: type TStringListHelper = class helper for TStringList function High: NativeInt; end; I getError: Identifier not found "class" I have the Classes unit. Is there something else I am missing? James Another alternative would be declaring a helper: type TStringListHelper = class helper for TStringList function High: NativeInt; end; function TStringListHelper.High: NativeInt; begin Exit (Self.Count-1); end; ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
On 10/09/2022 17:01, James Richters via fpc-pascal wrote: For Loop := 0 to MyStringList.Count-1 do It would be nice to not have the -1 I don't understand what is wrong with Count-1, but you can get the highest index like this if you wish: high_index:=Length(Trim(MyStringList.Text))-Length(StringReplace(Trim(MyStringList.Text),#13,'',[rfReplaceAll])); Martin. ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
Thank you for the example! I also needed {$Mode OBJFPC} to get it working. I am normally in {$Mode FPC} Or {$Mode TP} But I found that if I just put this in a unit that is {$Mode OBJFPC} and include that unit in my {$Mode TP} Unit it works just great! I have a useless unit that is just a whole list of constants, types, and variables used by my other units, just so I don't have to scroll past over 1000 lines of code just to get to the functions and procedures. It was {$Mode FPC} For some reason {$Mode FPC} {$modeswitch typehelpers} Still had Error: Identifier not found "class" So I changed it to {$Mode OBJFPC} {$modeswitch typehelpers} And put the function in it, and it's working great! My main reason for wanting something like this is because I occasionally will forget the -1 and... well... that's no good... Since I recently learned of High() and Low() for arrays, I've been changing all my for loops with arrays to use High() and sometimes Low() if they don't start at 0. It's also helpful to do something like: MyArray[High(MyArray)] := something; To write to a newly added array element where I used to have MyArray[Length(MyArray)-1] := something; While it does work.. it's less readable and prone to errors. James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
If you just don't like the "-1" for readability, you might also wan't to consider using for i := 0 to Pred(f.Count) do ... - Original Message - From: Thomas Kurz via fpc-pascal To: 'FPC-Pascal users discussions' Sent: Saturday, September 10, 2022, 21:37:34 Subject: [fpc-pascal] Get highest element of a StringList Try this (note the "modeswitch"): program Project1; {$modeswitch typehelpers} uses Classes, SysUtils; type TStringListHelper = class helper for TStringList function High: NativeInt; end; function TStringListHelper.High: NativeInt; begin Exit (Self.Count-1); end; var f: TStringList = nil; i: Integer; begin f := TStringList.Create; f.Add ('hallo'); f.Add ('welt'); f.Add ('!'); for i := 0 to f.High do Writeln (f[i]); FreeAndNil (f); end. - Original Message - From: James Richters via fpc-pascal To: 'FPC-Pascal users discussions' Sent: Saturday, September 10, 2022, 19:57:51 Subject: [fpc-pascal] Get highest element of a StringList This Helper sounds like a nice way to do it. I need the numerical index of the loop so this would be more straight forward. Would I then do something like : For I:= 0 to MyStringList.High Do ? When I try to add the Type statement: type TStringListHelper = class helper for TStringList function High: NativeInt; end; I getError: Identifier not found "class" I have the Classes unit. Is there something else I am missing? James >Another alternative would be declaring a helper: >type TStringListHelper = class helper for TStringList function High: NativeInt; end; >function TStringListHelper.High: NativeInt; begin > Exit (Self.Count-1); >end; ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
Try this (note the "modeswitch"): program Project1; {$modeswitch typehelpers} uses Classes, SysUtils; type TStringListHelper = class helper for TStringList function High: NativeInt; end; function TStringListHelper.High: NativeInt; begin Exit (Self.Count-1); end; var f: TStringList = nil; i: Integer; begin f := TStringList.Create; f.Add ('hallo'); f.Add ('welt'); f.Add ('!'); for i := 0 to f.High do Writeln (f[i]); FreeAndNil (f); end. - Original Message - From: James Richters via fpc-pascal To: 'FPC-Pascal users discussions' Sent: Saturday, September 10, 2022, 19:57:51 Subject: [fpc-pascal] Get highest element of a StringList This Helper sounds like a nice way to do it. I need the numerical index of the loop so this would be more straight forward. Would I then do something like : For I:= 0 to MyStringList.High Do ? When I try to add the Type statement: type TStringListHelper = class helper for TStringList function High: NativeInt; end; I getError: Identifier not found "class" I have the Classes unit. Is there something else I am missing? James >Another alternative would be declaring a helper: >type TStringListHelper = class helper for TStringList function High: NativeInt; end; >function TStringListHelper.High: NativeInt; begin > Exit (Self.Count-1); >end; ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
This Helper sounds like a nice way to do it. I need the numerical index of the loop so this would be more straight forward. Would I then do something like : For I:= 0 to MyStringList.High Do ? When I try to add the Type statement: type TStringListHelper = class helper for TStringList function High: NativeInt; end; I getError: Identifier not found "class" I have the Classes unit. Is there something else I am missing? James >Another alternative would be declaring a helper: >type TStringListHelper = class helper for TStringList function High: NativeInt; end; >function TStringListHelper.High: NativeInt; begin > Exit (Self.Count-1); >end; ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
Another alternative would be declaring a helper: type TStringListHelper = class helper for TStringList function High: NativeInt; end; function TStringListHelper.High: NativeInt; begin Exit (Self.Count-1); end; - Original Message - From: James Richters via fpc-pascal To: 'FPC-Pascal users discussions' Sent: Saturday, September 10, 2022, 18:01:20 Subject: [fpc-pascal] Get highest element of a StringList I thought I would try: For Loop := 0 to High(MyStringList) do But I get "Error: Type mismatch" Is there a way to get the highest element of a stringlist other than: For Loop := 0 to MyStringList.Count-1 do It would be nice to not have the -1 Could High() be made to work if the argument was a stringlist? James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Get highest element of a StringList
On Sat, Sep 10, 2022 at 6:01 PM James Richters via fpc-pascal wrote: > Is there a way to get the highest element of a stringlist other than: > > For Loop := 0 to MyStringList.Count-1 do You can use the for .. in loop. > It would be nice to not have the -1 > Could High() be made to work if the argument was a stringlist? Probabaly it could, but it's not likely going to happen. -- Bart ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
[fpc-pascal] Get highest element of a StringList
I thought I would try: For Loop := 0 to High(MyStringList) do But I get "Error: Type mismatch" Is there a way to get the highest element of a stringlist other than: For Loop := 0 to MyStringList.Count-1 do It would be nice to not have the -1 Could High() be made to work if the argument was a stringlist? James ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Shift key with PTCCRT
Thanks for the suggestion I think the syntax should be: type myKeyEvent = IPTCKeyEvent; Var myShiftStatus : boolean; myShiftStatus := myKeyEvent.Shift; but I get IPTCKeyEvent not found. I wonder if it's only designated as internal.. or if I need to use something other than PTCGraph and PTCCRT James >looking at the list of constants in ptcpas, i find PTCKEY_SHIFT... i also find, in Classes, the IPTCKeyEvent which has a GetShift function as well as a Shift >property... seems like you should be able to the shift status with something like >type myKeyEvent : IPTCKeyEvent; >type myShiftStatus : boolean; >myShiftStatus := myKeyEvent.Shift; ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Re: [fpc-pascal] Getting Shift key with PTCCRT
On 9/9/22 5:54 PM, James Richters via fpc-pascal wrote: I have some key sequences with PTCCRT.READKEY where I use a lower case letter to do one thing and an uppercase to do another, but if the Capslock is on, they do the wrong things. Is there a way I can check the condition of the shift key to see if shift was actually pressed instead of just going by whether I got a capital letter or not? The key input is not something that is ever displayed on the screen, it's just making selections and doing them. looking at the list of constants in ptcpas, i find PTCKEY_SHIFT... i also find, in Classes, the IPTCKeyEvent which has a GetShift function as well as a Shift property... seems like you should be able to the shift status with something like type myKeyEvent : IPTCKeyEvent; type myShiftStatus : boolean; myShiftStatus := myKeyEvent.Shift; https://ptcpas.sourceforge.io/api-reference/ptc/iptckeyevent.html NOTE: it has been way too long since i've actually written any pascal code... take the above with a grain of salt... i may have the syntax all wrong... -- NOTE: No off-list assistance is given without prior approval. *Please keep mailing list traffic on the list where it belongs!* ___ fpc-pascal maillist - fpc-pascal@lists.freepascal.org https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal