Recent

Author Topic: Help locating program logic  (Read 5186 times)

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Help locating program logic
« on: February 17, 2019, 07:48:00 am »
I Have a small program with three listboxes, Listbox1, Listbox2,  Listbox3,

At the upper right of the form  there is a button labeled 'Path'. It hard coded to a file which is in  the program location.

 File Name is: 'xxxtapt.dat' and it has has 3 records.

When the program processes the 3 three records the Listbox's should look as follows:

ListBox1         ListBox2      ListBox3

LELG              LELG             K3JC
                                          TN04

Instead they look like this:

ListBox1         ListBox2      ListBox3
K3JC                LELG             K3JC
LELG                                    TN04

I'm trying to get data with numbers in listbox3 and data with all characters in listbox1 and listbox2.

The main processing is between line 189 and 244 inside a case statement.

I can't figure out how to make it work right. I have been all day on it and I just can't see it.

The program and data file are attached.

Just unzip anywhere and it should run. Just not the way I would like it to.

Can someone take a look.

Thanks,


FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: Help locating program logic
« Reply #1 on: February 17, 2019, 08:48:23 am »
I Have a small program with three listboxes, Listbox1, Listbox2,  Listbox3,

At the upper right of the form  there is a button labeled 'Path'. It hard coded to a file which is in  the program location.

 File Name is: 'xxxtapt.dat' and it has has 3 records.

When the program processes the 3 three records the Listbox's should look as follows:

ListBox1         ListBox2      ListBox3

LELG              LELG             K3JC
                                          TN04

Instead they look like this:

ListBox1         ListBox2      ListBox3
K3JC                LELG             K3JC
LELG                                    TN04

I'm trying to get data with numbers in listbox3 and data with all characters in listbox1 and listbox2.

The main processing is between line 189 and 244 inside a case statement.

I can't figure out how to make it work right. I have been all day on it and I just can't see it.

The program and data file are attached.

Just unzip anywhere and it should run. Just not the way I would like it to.

Can someone take a look.

Thanks,
It seems your logic is unnecessarily complex.
Maybe:
Code: Pascal  [Select][+][-]
  1.         Reset(DataFile);
  2.         while not eof(DataFile) do
  3.         begin
  4.           Readln(DataFile, Line);
  5.           edLBTwo.Text := Line;
  6.           RCD := Copy2Space(Line);
  7.           iApt := ExtractWord(5, Line, [' ']);
  8.  
  9.                    // Same logic was repeated three times unnecessarily...
  10.                    EmptyStr := EmptyString(iApt);
  11.                    if EmptyStr then begin Continue; end;
  12.  
  13.                    Numeric := NumericData(iApt);
  14.                    if Numeric then begin
  15.                     ListBox3.Items.Add(iApt);
  16.                     Continue;
  17.                    end;
  18.  
  19.                    AllCapLtr := AllCapsChar(iApt);
  20.                    if ((not (Numeric))  and (AllCapLtr)) then
  21.                    begin
  22.                       ListBox1.Items.Add(iApt);
  23.                       ListBox2.Items.Add(iApt);
  24.                       Continue;
  25.                    end;
  26.  
  27.           Case RCD of
  28.            '1'  : begin
  29.                      // There was nothing different here...
  30.                    end;
  31.  
  32.            '16' : Begin
  33.                      // There was nothing different here...
  34.                  end;
  35.  
  36.            '17' : begin
  37.                      // There was nothing different here...
  38.                    end;
  39.           end;
  40.         end;
  41.         CloseFile(DataFile);
  42.  

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #2 on: February 17, 2019, 08:56:28 am »
Not really.
 It may seem that way but the actual file I'm processing has a lot of records. The ones I'm interested it are the ones starting with a 1, 16 and 17.

That is why I have the case statement. To trap those records and determine if the field I'am interested in is blank, numeric or all letters. All the rest of the records I read are ignored.

The file I supplied is a test data file to test the logic. 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

Zvoni

  • Hero Member
  • *****
  • Posts: 2319
Re: Help locating program logic
« Reply #3 on: February 17, 2019, 01:25:38 pm »
If you're trying to figure out if an extracted Word contains digits, why not throw a Regex against it?
Or as a second idea: Why not process the extracted word character by charatcer and looking up if it's in '0123456789'?
If yes, load into listbox3 else add to Listbox1 and 2
Aircode!
Code: [Select]
Found:=False;
For i:=1 to 4 do
  begin
    If Pos(Word[i], '0123456789')>0 Then
      Begin
        Found:=True;
        BreaK;
      End;
    End;

If Found Then
    Listbox3.Items.Add(Word)
Else
   Begin
      Listbox1.Items.Add(Word);
      Listbox2.Items.Add(Word);
   End;
End;

« Last Edit: February 17, 2019, 01:28:07 pm by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Help locating program logic
« Reply #4 on: February 17, 2019, 01:43:17 pm »
Quicker (and cleaner) if you do it like this:
Code: Pascal  [Select][+][-]
  1. function ContainsDigit(AWord: RawByteString): Boolean;
  2. const Digits = ['0'..'9'];
  3. var i: Integer;
  4. begin
  5.   Result := False;
  6.   For i:=1 to Length(AWord) do
  7.     If AWord[i] in Digits then begin
  8.       Result := True;
  9.       BreaK;
  10.     end;
  11. end;
  12.  
  13. {... and elsewhere ...}
  14.   if ContainsDigit(TheWord) then
  15.     Listbox3.Items.Add(TheWord)
  16.   else begin
  17.     Listbox1.Items.Add(TheWord);
  18.     Listbox2.Items.Add(TheWord);
  19.   end;
  20. end;
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #5 on: February 17, 2019, 04:15:21 pm »
@lucmar

I'll try it.

Thanks.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #6 on: February 17, 2019, 04:31:14 pm »
I Have a small program with three listboxes, Listbox1, Listbox2,  Listbox3,

At the upper right of the form  there is a button labeled 'Path'. It hard coded to a file which is in  the program location.

 File Name is: 'xxxtapt.dat' and it has has 3 records.

When the program processes the 3 three records the Listbox's should look as follows:

ListBox1         ListBox2      ListBox3

LELG              LELG             K3JC
                                          TN04

Instead they look like this:

ListBox1         ListBox2      ListBox3
K3JC                LELG             K3JC
LELG                                    TN04

I'm trying to get data with numbers in listbox3 and data with all characters in listbox1 and listbox2.

The main processing is between line 189 and 244 inside a case statement.

I can't figure out how to make it work right. I have been all day on it and I just can't see it.

The program and data file are attached.

Just unzip anywhere and it should run. Just not the way I would like it to.

Can someone take a look.

Thanks,
It seems your logic is unnecessarily complex.
Maybe:
Code: Pascal  [Select][+][-]
  1.         Reset(DataFile);
  2.         while not eof(DataFile) do
  3.         begin
  4.           Readln(DataFile, Line);
  5.           edLBTwo.Text := Line;
  6.           RCD := Copy2Space(Line);
  7.           iApt := ExtractWord(5, Line, [' ']);
  8.  
  9.                    // Same logic was repeated three times unnecessarily...
  10.                    EmptyStr := EmptyString(iApt);
  11.                    if EmptyStr then begin Continue; end;
  12.  
  13.                    Numeric := NumericData(iApt);
  14.                    if Numeric then begin
  15.                     ListBox3.Items.Add(iApt);
  16.                     Continue;
  17.                    end;
  18.  
  19.                    AllCapLtr := AllCapsChar(iApt);
  20.                    if ((not (Numeric))  and (AllCapLtr)) then
  21.                    begin
  22.                       ListBox1.Items.Add(iApt);
  23.                       ListBox2.Items.Add(iApt);
  24.                       Continue;
  25.                    end;
  26.  
  27.           Case RCD of
  28.            '1'  : begin
  29.                      // There was nothing different here...
  30.                    end;
  31.  
  32.            '16' : Begin
  33.                      // There was nothing different here...
  34.                  end;
  35.  
  36.            '17' : begin
  37.                      // There was nothing different here...
  38.                    end;
  39.           end;
  40.         end;
  41.         CloseFile(DataFile);
  42.  

Attached is a more realistic data set for the program.

Between the '1' record indicator there are several records which I'm not interested in. That is why I need the complicate case statement to filter out the unwanted records.

If I used your code example I would process all the records in the file.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #7 on: February 17, 2019, 05:25:16 pm »
@lucamar

I can't make the code example compile.

do I need something added to the use clause. I have it in an example program and it attached. 
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Help locating program logic
« Reply #8 on: February 17, 2019, 06:32:00 pm »
The "...and elsewhere..." comment and following lines are meant to show how to call the function not to be copied verbatim!

Also, you added it after the final "end."!

And there's no listbox in that example neither any place where you get words from the line, or ...

Beyond that, there were lots of other errors and the code was very badly formatted. In the end I made a few changes, cleaned it up and tested its compilability : find it attached.

HTH!

ETA Oops! Introduced a bug :-[ Updated.
« Last Edit: February 17, 2019, 06:38:08 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Help locating program logic
« Reply #9 on: February 17, 2019, 06:50:07 pm »
      if ((not (Numeric))  and (AllCapLtr)) then

To
     If (Not numeric) and (AllCapLtr) then....

The only true wisdom is knowing you know nothing

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #10 on: February 17, 2019, 06:55:30 pm »
Yea, "The "...and elsewhere..." comment and following lines are meant to show how to call the function not to be copied verbatim!"

Yea I saw that after I posted. I commented it out in my code. It was copied into the program to remind me to make the changes.

Should have previewed the post.

Thanks

FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #11 on: February 17, 2019, 07:20:50 pm »
      if ((not (Numeric))  and (AllCapLtr)) then

To
     If (Not numeric) and (AllCapLtr) then....

Tried it and didn't work
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #12 on: February 17, 2019, 09:05:14 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2.  Var
  3.   TheWord: RawByteString;
  4.   iDigits : Boolean;
  5.   iApt : String;
  6. begin
  7. //============= Returns True =============================
  8.   iDigits := False;
  9.   iApt := ExtractWord(5, sTest2, [' ']);  //  iApt = TN04
  10.   Label22.Caption := iApt;
  11.   TheWord := iApt;
  12.   Label14.Caption := iApt;
  13.   if ContainsDigit(TheWord) then begin iDigits := True end;
  14.   if iDigits then
  15.    Label12.Caption := 'True'
  16.    else
  17.    Label12.Caption := 'False';
  18.  //============= Returns True =========================
  19.  iDigits := False;
  20.  iApt := ExtractWord(5, sTest3, [' ']);  //  iApt = NZCB
  21.  Label18.Caption := iApt;
  22.  if ContainsDigit(TheWord) then begin iDigits := True; end;
  23.  
  24.  if iDigits then
  25.   Label20.Caption := 'True'
  26.   else
  27.   Label20.Caption := 'False';
  28.  
  29. end;    

I implemented the code and ContainsDigit always returns True as far as I can tell.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Help locating program logic
« Reply #13 on: February 17, 2019, 09:59:27 pm »
No, it doesn't--ok, shouldn't. Bared almost to the minimum the test may be:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button1Click(Sender: TObject);
  2. var
  3.   iDigits : Boolean;
  4.   iApt : String;
  5. begin
  6. //============= Returns True =============================
  7.   iApt := 'TN04';
  8.   iDigits := ContainsDigit(iApt);
  9.   if iDigits then
  10.     ShowMessage(iApt + ' contains digit(s)')
  11.   else
  12.     ShowMessage(iApt + ' contains NO digit');
  13.  //============= Returns True? =========================
  14.  iDigits := False;
  15.  iApt := 'NZCB';
  16.  iDigits := ContainsDigit(iApt);
  17.  if iDigits then
  18.    ShowMessage(iApt + ' contains digit(s)')
  19.  else
  20.    ShowMessage(iApt + ' contains NO digit')
  21. end;

Results as shown in the attached images. The problem in your code is that you're forgetting to reassign iApt to TheWord the second time:
Code: [Select]
iDigits := False;
 iApt := ExtractWord(5, sTest3, [' ']);  //  iApt = NZCB
 Label18.Caption := iApt;
 {*** Here TheWord still contains 'TN04' ***}
 if ContainsDigit(TheWord) then begin iDigits := True;
end

Note that you don't need that
Code: [Select]
TheWord: RawByteString;; you can pass any string to ContainsDigit(). The parameter is declared RawByteString to avoid any conversion of the string from one CP to another.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

JLWest

  • Hero Member
  • *****
  • Posts: 1293
Re: Help locating program logic
« Reply #14 on: February 17, 2019, 11:01:07 pm »
@lucamar

Yea, that works.

I must have something wrong in my code..

Thanks.
FPC 3.2.0, Lazarus IDE v2.0.4
 Windows 10 Pro 32-GB
 Intel i7 770K CPU 4.2GHz 32702MB Ram
GeForce GTX 1080 Graphics - 8 Gig
4.1 TB

 

TinyPortal © 2005-2018