Post Reply 
Bug? Tricky syntax? Finding items in a list
11-03-2017, 03:28 PM (This post was last modified: 11-03-2017 09:16 PM by StephenG1CMZ.)
Post: #1
Bug? Tricky syntax? Finding items in a list
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.

Stephen Lewkowicz (G1CMZ)
https://my.numworks.com/python/steveg1cmz
Visit this user's website Find all posts by this user
Quote this message in a reply
Post Reply 


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



User(s) browsing this thread: 1 Guest(s)