Recent

Author Topic: Application - recommendations?  (Read 13282 times)

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Application - recommendations?
« Reply #15 on: September 25, 2017, 06:56:58 pm »
Would it be possible to use a set of integers instead of bytes and avoid the casting? (not that I think it will make a difference but casting can theoretically lead to problems).
No, sets in Pascal are limited to 256 elements, i.e. set of Byte is the largest set possible, if you use the set constructor and operators that are built-in to the language. (You could of course devise your own implementation of a set of Integers, but it would be slow compared to the built-in Pascal implementation of the smaller sized set).

Casting is OK as long as you know what you are doing. In this case casting from Integer to Byte is perfectly OK because we know that the r (Integer) value will never be negative or greater than 40. i.e. it is always within Byte range. So the cast to Byte is always safe. As I said, it would have been better really to declare r as Byte in the first place from an educational point of view. I unconsciously declared it as Integer because I know that native types are usually processed  fastest. But in this case speed is not an issue at all, and clarity of code should have led me to declare r as a Byte (which would have avoided this discussion).

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Application - recommendations?
« Reply #16 on: September 25, 2017, 07:27:31 pm »
It's all built into TStringlist. Use two stringlists, the first one reads the file, the seconds one gets the lines via its property DelimitedText. If the Delimiter of the 2nd stringlist is the delimiter used between the columns of the csv file then this operation splits up each line into its records.

Check this out:

Elegant solution! Thanks!

mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: Application - recommendations?
« Reply #17 on: September 25, 2017, 08:51:19 pm »
I have one last trick that you can use with the Tstring approach.  you can use it again to pull out several items from each line so you can have several parameters for each student. When you read from file, you'll get a tstring item/line in the file.  I my trick is...
Code: [Select]
procedure foo(ts_file:Tstringlist; line:integer);
var str:string;
    tstr:tstringlist;
    rec:TStudent_record;
begin
  tstr := Tstringlist.create();
  tstr.delimiter :=',';  //
  try
     str := ts_file.string[line];  // get the full record and move into string
     tstr.commatext := str;     // move it into a new tstring so it's sorted into separate list.
     if (tstr.count > 0) then
        rec.frist_name_str := tstr.strings[0];
     if (tstr.count > 1) then
        rec.last_name_str := tstr.strings[1];
     if (tstr.count > 2) then
        rec.studant_number := strtoint(tstr.strings[2]);
     if (tstr.count > 3) then
        rec.lastChap_int := strtoint(tstr.strings[2]);
    ... and on and on
   
  finally 
    tstr.free;
  end;
end;

I like to use the 2 step approce "str:= x" and then "y:=str" so I can look at the data while I am debugging.
And there are of course better ways to convert string to int but this gives you the general ideal.  I like the "Trystrtoint()"  or whatever it's name is so you do not need to deal with exceptions if the text you are converting is not a number. 
windows 7/10 - laz 2.0 / 1.2.6 general releases

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Application - recommendations?
« Reply #18 on: September 26, 2017, 04:55:21 pm »

Could you give me hints on how to create my own simple parser?


Enjoy! http://wiki.freepascal.org/CsvDocument
Be mindful and excellent with each other.
https://github.com/cpicanco/

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Application - recommendations?
« Reply #19 on: September 26, 2017, 05:04:11 pm »
If you need custom access you don't want a custom parser. Use TCSVDocument instead:

Code: Pascal  [Select][+][-]
  1. var
  2.   MyText : string;
  3. begin
  4.   Document := TCSVDocument.Create;
  5.   try
  6.     Document.LoadFromFile(Filename);
  7.     MyText := Document.Cells[ 0 {column},  0 {row}];
  8.   finally
  9.     Document.Free;
  10.   end;
  11. end;
Be mindful and excellent with each other.
https://github.com/cpicanco/

Thaddy

  • Hero Member
  • *****
  • Posts: 14157
  • Probably until I exterminate Putin.
Re: Application - recommendations?
« Reply #20 on: September 26, 2017, 05:08:32 pm »
I happened to catch your not using free. But now it looks ok.
« Last Edit: September 26, 2017, 05:52:20 pm by Thaddy »
Specialize a type, not a var.

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Application - recommendations?
« Reply #21 on: September 26, 2017, 08:38:47 pm »
Also, for csv I would suggest csvdocument unit. It has a parser for loading stuff (TCSVParser) and a class for random access to cells TCSVDocument.

TCSVDocument is very simple and strainght to the point. All you need is to load/save csv files, right?

I searched for TCSVDocument documentation without much luck. This page http://wiki.freepascal.org/CsvDocument is outdated. Also, is there a difference between TCSVDocument and CsvDocument?

(Use the source (Luke?)). Then I searched the source for a .pp file. I didn't find a TCSVDocument, but a csvdocument.pp file in path:

C:\apps\lazarus\fpc\3.0.2\source\packages\fcl-base\src

Searching the file for string "header" doesn't provide any hits. Does it even handle csv headers? The second most used feature of csv files after field and line separators. 

But maybe I didn't search the right places for documentation?

Kind regards,
Michael

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Application - recommendations?
« Reply #22 on: September 26, 2017, 08:48:35 pm »
I happened to catch your not using free. But now it looks ok.

?

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Application - recommendations?
« Reply #23 on: September 26, 2017, 09:09:58 pm »
Would it be possible to use a set of integers instead of bytes and avoid the casting? (not that I think it will make a difference but casting can theoretically lead to problems).
No, sets in Pascal are limited to 256 elements, i.e. set of Byte is the largest set possible, if you use the set constructor and operators that are built-in to the language. (You could of course devise your own implementation of a set of Integers, but it would be slow compared to the built-in Pascal implementation of the smaller sized set).

Got it, thanks!

The documentation on sets is a little sparse on examples, like your example where the initial set is empty http://wiki.freepascal.org/Set. Where do you guys search for inspiration, guides etc? (Besides this great and very responsive community?)

Other doc mentions a max of 255 elements: 
https://www.freepascal.org/docs-html/ref/refsu16.html#x40-580003.3.3
« Last Edit: September 26, 2017, 09:18:09 pm by mijen67 »

howardpc

  • Hero Member
  • *****
  • Posts: 4144
Re: Application - recommendations?
« Reply #24 on: September 26, 2017, 09:49:18 pm »
The documentation on sets is a little sparse on examples, like your example where the initial set is empty http://wiki.freepascal.org/Set. Where do you guys search for inspiration, guides etc? (Besides this great and very responsive community?)

Other doc mentions a max of 255 elements: 
https://www.freepascal.org/docs-html/ref/refsu16.html#x40-580003.3.3

The docs speak about a range of elements from 0 to 255 - which is 256 elements.
I've found the best examples are often bits of code shared on this forum, together with browsing the RTL/FCL and LCL sources.
The LCL sources are forbidding to start with. But bit by bit you come to see why a developer who is more skilled than you chose to implement something the way he did.
The IDE makes it so easy to trace the ancestry of methods (Alt-Up arrow, possibly repeated) that you have no excuse for not exploring, at least a bit.
Most Pascal tutorials will include a section on sets because sets are a major feature of the language (a feature not shared by several other popular languages).

mas steindorff

  • Hero Member
  • *****
  • Posts: 526
Re: Application - recommendations?
« Reply #25 on: September 26, 2017, 10:27:14 pm »
sets give you a readable variable value and some range testing.  I think if I needed more than 256 of them, I would check out the " specialized TFPGMap<string,integer>;" option.
in my code I can do..
Code: [Select]
type
TSmap=  specialized TFPGMap<string,integer>
var Slmap :TSmap;
then in the initialization I load the string to integer conversion array with my list like
Code: [Select]
initialization
  Slmap := TsMap.create
  Slmap['quiz_1'] := 7;
  Slmap['quiz_2'] := 8;
  ...
  Slmap['test_1']:= 101;
  Slmap['test_2']:= 102;
  Slmap['midterm'] := 102; // test_2 is the same as the midterm test
  ...
I make this a separate unit to simplify.  in the main code I can now use strings as integers
 
nextTask := Slmap['test_1'];

note: you may need to keep all of your strings upper or lower case.

before this, I would create a const record with each of it's members assigned a value.  then autocomplete would help me...

newtask := tasktypes.prepare4test;   as an example


windows 7/10 - laz 2.0 / 1.2.6 general releases

cpicanco

  • Hero Member
  • *****
  • Posts: 618
  • Behavioral Scientist and Programmer
    • Portfolio
Re: Application - recommendations?
« Reply #26 on: September 26, 2017, 11:55:55 pm »
Quote
This page http://wiki.freepascal.org/CsvDocument is outdated

I can confirm that the parser example in this outdated page works. csvdocument is the unit and TCSVDocument is the class inside this unit.
Be mindful and excellent with each other.
https://github.com/cpicanco/

wp

  • Hero Member
  • *****
  • Posts: 11830
Re: Application - recommendations?
« Reply #27 on: September 27, 2017, 12:34:57 am »
Quote
This page http://wiki.freepascal.org/CsvDocument is outdated

I can confirm that the parser example in this outdated page works. csvdocument is the unit and TCSVDocument is the class inside this unit.

The wiki page has been marked as being outdated because it refers to the version in Lazarus Components And Code Repository, but csvdocument is now part of the FPC distribution (packages/fcl-base/src). The only major difference is that reading/writing/parsing has been split off into a separate unit, csvreadwrite. TCSVDocument is still in unit csvdocument. It is still sufficient to "use" csvdocument because this unit loads the TCSVParser automatically from csvreadwrite.

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Application - recommendations?
« Reply #28 on: September 29, 2017, 07:37:37 pm »
I have one last trick that you can use with the Tstring approach.  you can use it again to pull out several items from each line so you can have several parameters for each student. When you read from file, you'll get a tstring item/line in the file.  I my trick is...

Thanks! I've adapted this method!

mijen67

  • Full Member
  • ***
  • Posts: 130
  • It's hard to beat the bandwidth of a flying DVD
Re: Application - recommendations?
« Reply #29 on: September 29, 2017, 07:44:15 pm »
The attached project shows one way to go about this task.

I noted that you used:

{$ModeSwitch advancedrecords}

and records (TStudent and TStudents) with "embedded" functions/procedures.

May I ask why you didn't go "all the way" and used a class?

Kind regards,
Michael

 

TinyPortal © 2005-2018