HP Forums
Bug? Tricky syntax? Finding items in a list - Printable Version

+- HP Forums (https://www.hpmuseum.org/forum)
+-- Forum: HP Calculators (and very old HP Computers) (/forum-3.html)
+--- Forum: HP Prime (/forum-5.html)
+--- Thread: Bug? Tricky syntax? Finding items in a list (/thread-9431.html)



Bug? Tricky syntax? Finding items in a list - StephenG1CMZ - 11-03-2017 03:28 PM

This program is meant to find the positions of items in a list.
A simple FOR loop, yes?
No, it has problems if the list contains lists.
It gives a Bad Agument Error...unless you insert a diagnostic print, in which case the inner list is printed, and nothing after that.

Code:



 EXPORT ListFIND(LST,ITEM)
 //Find all instances of ITEM
 BEGIN
  LOCAL II;
  LOCAL LSTPOSNS:={};
  PRINT();
  IF SIZE(LST) THEN //
   //LOOP COULD BE OPTIMISED BY USING POS TO LEAP TO NEXT MODE
   FOR II FROM 1 TO SIZE(LST) DO
    //THIS LOOKS EASY BUT FAILS IF LST CONTAINS A LIST
    //EG ({1,{},3,4},3) BAD ARGUMENT TYPE
    //BOTH EQ AND == FAIL

     //WITH PRINT:NO II=3 PRINTED
     //WITHOUT PRINT: BAD ARGUMENT ERROR
    //PRINT({II,LST(II)});
    IF EQ(LST(II),ITEM) THEN
     LSTPOSNS(0):=II;
    END;//IF 
   END;//FOR
  END;//IF
  //Return Positions that match ITEM
  RETURN LSTPOSNS;
 END;

EXPORT FINDER()
BEGIN
  MSGBOX(ListFIND({1,{},3,4},3));
END;

Example: Find({1,{},33,4},33) should yield {3}
But (1,{} ,3,4},3 fails.


RE: Bug? Tricky syntax? Finding items in a list - Carlos295pz - 11-03-2017 03:53 PM

Code:

EXPORT ListFIND(LST,ITEM)
BEGIN
  LOCAL II;
  LOCAL LSTPOSNS:={};
  PRINT();
  PRINT("List: "+STRING(LST));
  PRINT("FIND: "+STRING(ITEM)+"\n");


  FOR II FROM 1 TO SIZE(LST) DO
    IF TYPE(LST(II))=TYPE(ITEM) AND EQ(LST(II),ITEM) THEN
      LSTPOSNS(0):=II;
      PRINT("Pos: "+II)
    END
  END;

  RETURN LSTPOSNS;

END;



RE: Bug? Tricky syntax? Finding items in a list - Carlos295pz - 11-03-2017 04:19 PM

This is a little faster

Code:

EXPORT ListFIND2(LST,ITEM)
BEGIN
  LOCAL X,Y;
  LOCAL LSTPOSNS={};

  WHILE X:=POS(LST,ITEM) DO
    LSTPOSNS(0):=(Y:=X+Y);
    LST:=LST({X,SIZE(LST)}+1)
  END;

  RETURN LSTPOSNS;

END;



RE: Bug? Tricky syntax? Finding items in a list - webmasterpdx - 11-04-2017 07:01 AM

There are 2 list libraries in the software libraries that you might find useful. The first is an extension to the list functions, and the second (which uses the first) is for associated lists (content addressable memory). Both are linked to from the Programming Documentation section of the wiki here...
http://www.wiki4hp.com/doku.php?id=prime:start

Here are the 2 links...

First is the Programmer's Expanded List Library...

http://www.hpmuseum.org/forum/thread-7001.html?highlight=list

Then there is the Associative List Library...

http://www.hpmuseum.org/forum/thread-9097.html?highlight=list


RE: Bug? Tricky syntax? Finding items in a list - StephenG1CMZ - 11-04-2017 09:36 AM

(11-03-2017 04:19 PM)Carlos295pz Wrote:  This is a little faster

Code:

EXPORT ListFIND2(LST,ITEM)
BEGIN
  LOCAL X,Y;
  LOCAL LSTPOSNS={};

  WHILE X:=POS(LST,ITEM) DO
    LSTPOSNS(0):=(Y:=X+Y);
    LST:=LST({X,SIZE(LST)}+1)
  END;

  RETURN LSTPOSNS;

END;

Yes...Actually, I see that as about 6 times faster, not just "a little".
I will include that version in my List API if I may.
http://www.hpmuseum.org/forum/thread-9411.html


RE: Bug? Tricky syntax? Finding items in a list - Didier Lachieze - 11-04-2017 09:42 AM

(11-03-2017 04:19 PM)Carlos295pz Wrote:  
Code:
    LST:=LST({X,SIZE(LST)}+1)

The +1 is a very nice trick !