Visual LISP, AutoLISP and General Customization
cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Associative List with Strings and Spaces.

1 REPLY 1
Reply
Message 1 of 2
Millerni456
728 Views, 1 Reply

Associative List with Strings and Spaces.

Hey everybody,

 

I'm making a program that reads a property file that follows the format:

"key=value"  for each line on the text file.

 

One specific line has the key "file" and the value is some file path that includes a space.

file=C:/my folder/my file.txt

 

Now I can extract what the key and value is for any given line in the file.

However, I want to put all of the data into an Associative List so that I will not need to keep opening the file each time I search for information.

 

I've been using the cons function in order to create a dotted list, which is succesful.  However I have an issue when I add this dotted list to the end of my associative list.

 

Suppose I have a key and a value already stored inside variables.  Below I have two lines of code, first the dotted list portion is printed, and works as expected.  The second line adds that dotted list to the associative list:

(princ "Cons: ")(princ (cons key val)) (princ "\n") ;Prints (file . C:/my folder/my file.txt)
(setq assocList (append assocList (cons key val))) ;Error message here.

 

The error that I receive says "; error: bad list: "C:/my folder/my file.txt"

 

 

EDIT:

As I was positing this I made a change to my code and resolved the error. I am still posting this for reference sake of somebody else with the same problem.

Here is the change:

(setq assocList (append assocList (list (cons key val))))

 Now, the dotted list is inserted properly and looks like this after printing the whole associative list:

((version . 1.0.1) (file . C:\my folder\my file.txt))

 

 

Hurray!

 

If you still have any comments or have a way of improving the code, I'm open for further discussion.

 

Thanks.

-Nicholas

 

 

 

 

1 REPLY 1
Message 2 of 2
Lee_Mac
in reply to: Millerni456


@Millerni456 wrote:

Here is the change:

(setq assocList (append assocList (list (cons key val))))
 

If you still have any comments or have a way of improving the code, I'm open for further discussion.


 

I would strongly recommend the use of cons over append, since the latter is incredibly slow and inefficient when used in a loop construct.

 

For your example:

 

(setq assocList (cons (cons key val) assocList))

 

If the order of the items needs to be retained (which I doubt, since this is an association list), a simple reverse will suffice after the list has been constructed - though, even with the inclusion of the reverse function, the code is still far more efficient than using append.

 

To demonstrate the difference in efficiency, consider the following two functions with identical output:

 

(defun append-it ( n / l )
    (repeat n
        (setq l (append l (list n))
              n (1- n)
        )
    )
l )
(defun cons-it ( n / l )
    (repeat n
        (setq l (cons n l)
              n (1- n)
        )
    )
    (reverse l)
)

 

Both will return a list of n numbers in reverse:

 

_$ (append-it 5)
(5 4 3 2 1)
_$ (cons-it 5)
(5 4 3 2 1)

 

However, now compare the difference in efficiency:

 

_$ (benchmark '((append-it 100)(cons-it 100)))
Benchmarking ................Elapsed milliseconds / relative speed for 8192 iteration(s):

    (CONS-IT 100).......1232 / 5.86 <fastest>
    (APPEND-IT 100).....7223 / 1.00 <slowest>

 The cons method (even including the reverse) is almost 6 times as fast as repeatedly append'ing the list!

Can't find what you're looking for? Ask the community or share your knowledge.

Post to forums  

Autodesk Design & Make Report

”Boost