Forum > General

Incorrect Proc being called ???

(1/10) > >>

J-G:
Due to the changing energy prices I'm updating my project which keeps a record of my costs. The first thing I feel is sensible is to change some static arrays to dynamic. Two (out of three) were straightforward but the third is causing me some grief!

The arrays are concerned with energy tariffs  -  Gas, Electric & Solar Panel  - so naturally I have three small procedures to save each array to a (separate) file  -  viz.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---procedure SaveElecRates;Var  i : byte;begin  rewrite(ElecRates);  for i := 1 to MaxERec do      write(ElecRates,ElecList[i]);  close(ElecRates);end;procedure SaveGasRates;Var  i : byte;begin  rewrite(Gas_Rates);  for i := 1 to MaxGRec do      write(Gas_Rates,Gas_List[i]);  close(Gas_Rates);end;procedure SaveFiTRates;Var  i : byte;begin  rewrite(FiT_Rates);  for i := 1 to MaxFRec do      write(Fit_Rates,Fit_List[i]);  close(FiT_Rates);end;
Nothing extraordinary I'm sure you'll agree.

However, I've converted Solar (FiT) & Gas without a hitch but the call to SaveElecRates actually goes to SaveGasRates.   The CallStack shows the line number for the Gas Proc.  ???

I've saved the source, closed Lazarus, reopened and it continues to call Gas not Elec.

Can anyone suggest why this might be ?

Here is the 'calling' code:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  procedure ReadElec;  Var    i: word;  begin    {$I-} reset(ElecFile); {$I+}[...]        while not eof(ElecRates) do          begin[...]            read(ElecRates,ElecList[i]);[...]                  inc(i);          end;          close(ElecRates);     //  one off - conversion from Static to Dynamic Array         SaveElecRates; 

dseligo:
One thing I usually do when something strange as this is going on, is to do project 'Clean up and Build' (but I must admit that doesn't help in most cases, because the reason is my error somewhere in the project).

You mention you changed from static to dynamic arrays.
But your code is like this:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i := 1 to MaxERec do
Dynamic arrays goes from 0 to array length - 1. Check if you didn't make some error with array indexing.

And I usually use CloseFile instead of Close.

Arioch:
any array - dynamic, static, open, generic - any - goes as


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var   data: TArray<string>;  index: integer; item: string;begin      for index := Low(Data) to High(Data) do       xxx(data[index]);       for item in Data do       xxx(item); 
Andyou don't need to think about and memorize those constants any more

MarkMLl:

--- Quote from: dseligo on September 23, 2022, 09:03:06 pm ---One thing I usually do when something strange as this is going on, is to do project 'Clean up and Build'

--- End quote ---

Yes, definitely. There's an issue lurking in the compiler related to multi-unit rebuilds etc. that nobody's been able to duplicate to an adequate extent.

MarkMLl

wp:

--- Quote from: J-G on September 23, 2022, 07:56:59 pm ---
--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  procedure ReadElec;  Var    i: word;  begin    {$I-} reset(ElecFile); {$I+}[...]        while not eof(ElecRates) do          begin[...]            read(ElecRates,ElecList[i]);[...]                  inc(i);          end;          close(ElecRates);     //  one off - conversion from Static to Dynamic Array         SaveElecRates; 
--- End quote ---
Unless you did this somewhere else, I am missing a call to SetLength() here (as well as initialization of the count variable i). Since you probably do not know how many items will be found in the files you have to do this continually while reading, or in blocks. Here is the block solution:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  BLOCK_SIZE = 1000;var  i: Integer;begin  Reset(ElecRates);  i := 0;  while not EoF(ElecRates) do  begin    // When a block is full allocate another one. Each block consists of BLOCK_SIZE elements.    if i mod BLOCK_SIZE = 0 then      SetLength(ElecList, Length(ElecList) + BLOCK_SIZE);    Read(ElecRates, ElecList[i]);  // Using Read rather than ReadLn means that all elements are in a single line. Is this true?    inc(i);  end;  CloseFile(ElecRates);  // Remove the excess-allocated elements. i is the true number of elements.  SetLength(ElecList, i);end;

Navigation

[0] Message Index

[#] Next page

Go to full version