Help me understand please.

Started by mostlywrong, January 31, 2009, 01:17:23 PM

Previous topic - Next topic

mostlywrong


;Original copy/past from codepatterns
;(dolist (file-name (3 (main-args)))              
;    (set 'file (open file-name "read"))          
;    (println "file ---> " file-name)            
;    (while (read-line file)                      
;        (if (find (main-args 2) (current-line) 0)
;        (write-line)))                          
;    (close file))                                
;                                                
;(exit)                                          
;
;some small edits by me. and one error on purpose
(set 'file-name "h:/output.txt")
(dolist (filename (3 (main-args)))              
    (set 'file (open file-name "read"))          
    (println "file ---> " file-name)            
    (while (read-line file)                      
        ;(if (find (main-args 2) (current-line) 0)
        (if (find "Job:" (current-line) 0)
        (write-line)))
    (close file)
)


OK, I found this snip of code on the Code Patterns page.

It seems perfect for a start to a tiny project i want to do.

For my convenience i didn't want to type a file name every time. I looked and thought that by replaceing the (3 (main-args)) with the name of the file, in quotes. it would work.

I did sorta manage to get something to work but I'm not getting why it did not work.



I do see what the (3 (main-args)) statement does. Just not what happens at the next level up/out at the file-name.



I misspelled the symbol in the second section to because i could not get rid of it with out getting a ERR: symbol is protected in function dolist : set error.



edit:

    1 more question the (current-line) its apparent what it does, it return the current line. but im not seeing why?

is it part of the FIND ? or some other function?



edit2:

    Ok I kept looking up functions and i now see that "file-name" was/is the variable for theloop. It was expecting a list and i wasn't giving it one.

cormullion

#1
I think you just want this, without the dolist:


(set 'file-name "afile.txt")
(set 'file (open file-name "read"))
(println "file ---> " file-name)              
(while (read-line file)                      
   (if (find "Job" (current-line) 0)
       (write-line)))
(close file)

(exit)


since you're no longer passing parameters to the script.



Or try something like this - quicker up to a certain size of file:


(set 'file (parse (read-file "afile.txt") "n"))              
(dolist (line file)                      
   (if (find "set" line 0)
       (println line)))

mostlywrong

#2
thanks. what i want to do is this.

I want to find several strings grouped by "Job:"

    for example string1 to srting5

I will always find "Job:"

I will find 0 to 5 of the other strings.

at the next Job: repeat



I have a list of several jobs and i want to pull out several items and group them by job. seems simple enough. just haven't wrapped my brain around the lispy details yet.



Im not sure how to next the secondary string searches, break on the next "Job:' an still keep my position in the file.

Im thinking about just reading it in. at most it would be a couple of megs.



edit. your snipit is a big help. I think i see where i can nest the other finds.

mostlywrong

#3
i want to do this but dont see how.



if I find string1 and string2 println



I'm not getting the and relationship in the right place.





edit. got it. dropped a ")"