-1

How to get common words from two different sentences by using ENTRY function in progress4gl?

 define variable a1 as character  no-undo  initial  "hi dude do". 
 define variable a2 as character  no-undo  initial  "hi man it". 

 define variable cnta as character.
 define variable cntb as character.
 define variable cntc as character.

 define variable i as integer.
 define variable j as integer.

 do i = 1 to 3:

 entry (i,a1,"").

   do  j = 1 to 3:

    entry (j,a2,"").

  end.

 end.

/*   assign cntc = cnta matches cntb . */
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
sri
  • 11
  • 1
  • 7
  • 1. You provide input data, but not expected output data. Is the expected output "hi" ? 2. Why does the question attempt to restrict the function (ENTRY) that should be used to solve the problem? This nearly sounds like a job interview question. 3. What are cnta, cntb and cntc trying to illustrate? – Stefan Drissen Nov 15 '16 at 21:10
  • If we're going to answer interview questions do we get a signing bonus? – Tom Bascom Nov 16 '16 at 01:22

2 Answers2

1
define variable a1 as character  no-undo  initial  "hi dude do". 
define variable a2 as character  no-undo  initial  "hi man it". 

define variable common as character no-undo.

define variable cc as integer no-undo.
define variable ii as integer no-undo.
define variable jj as integer no-undo.

define variable n1 as integer no-undo.
define variable n2 as integer no-undo.

n1 = num-entries( a1 ).
n2 = num-entries( a2 ).

do ii = 1 to n1:

    do  jj = 1 to n2:

      if entry ( ii, a1, " ") = entry( jj, a2, " " ) then
        do:
          cc = cc + 1.
          common = common + " " + entry( ii, a1, " " ).
        end.

    end.

end.

display trim( cc ) common.

Notes:

The TRIM() function is just to clean up the "common" string so it doesn't have an extra space.

For performance reasons it is good to get in the habit of obtaining NUM-ENTRIES() outside the loop rather than with every iteration of the loop. It doesn't make much difference for small strings but for large strings it can have quite an impact.

Tom Bascom
  • 13,405
  • 2
  • 27
  • 33
  • 1
    The default delimiter for entry and num-entries is not a space but a comma. I would also put the trim outside the loop. – Stefan Drissen Nov 15 '16 at 06:29
  • 1
    Arg! You're right. I was thinking IMPORT & EXPORT – Tom Bascom Nov 15 '16 at 11:36
  • Looking at sri's two posts I might also note that "" <> " ". If the intent is that a space character should be taken as the delimiter between words then quote-space-quote is what needs to be specified for the delimiter argument. Not quote-quote. – Tom Bascom Nov 16 '16 at 15:19
-2

1./*if i need to get the n number of words in two sentences from the user at the run time , how to compare and the get the common words.

the following code compares and displays only first letter of the two sentence. */

define variable a1 as character  no-undo.
define variable a2 as character  no-undo.

define variable common as character no-undo.

define variable a1 as character FORMAT "x(64)" no-undo  /* initial  "hi d do" */. 
define variable a2 as character FORMAT "x(64)" no-undo  /* initial  "hi d it" */. 

define variable common as character FORMAT "x(64)" no-undo.
define variable c1 as character FORMAT "x(64)" no-undo.

 define variable x as character FORMAT "x(64)" no-undo.
 define variable y as character FORMAT "x(64)" no-undo.

define variable cc as integer no-undo initial 0.
define variable ii as integer no-undo.
define variable jj as integer no-undo.

define variable n1 as integer no-undo.
define variable n2 as integer no-undo.


set a1. 
n1 = num-entries( a1,"" ).


set a2. 
n2 = num-entries( a2,"" ).

do ii = 1 to n1:


   do  jj = 1 to n2:

   if entry ( ii,a1, " ") matches entry( jj,a2, " " ) then
   do:

     common = entry( ii, a1, " " ).
     display  common .     

   end.

   end.

end.
sri
  • 11
  • 1
  • 7
  • Are you asking a new question? – Tom Bascom Nov 15 '16 at 12:19
  • It is a bad idea to use MATCHES to test for simple equality. If you form a habit like that and then use it in a WHERE clause you will prevent index bracketing and cause a lot of table scans. – Tom Bascom Nov 15 '16 at 12:23