where am I messing up here?

Q&A's, tips, howto's
Locked
tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

where am I messing up here?

Post by tom »

I'm not doing something right, here. It's something
obvious, I'm sure..

Code: Select all

 
(set 'dir-path "/path/to/dir/") 
(set 'all-args (join (rest (rest (main-args))) " ")) 
(set 'bbook (append dir-path ((parse all-args) 0))) 
(set 'bverse ((parse all-args) 1))

(set 'file (open bbook "read")) 
(search file bverse)
(print (read-line file) "\n") 
(close file) 
Help?
:-)

cormullion
Posts: 2038
Joined: Tue Nov 29, 2005 8:28 pm
Location: latiitude 50N longitude 3W
Contact:

Post by cormullion »

I'm not sure you're missing anything. I think you're trying to search for a word or phrase in a file in a directory, and your code looks good to me.

Your approach works OK for me, although there was a small problem with a 'string too long' error, which was caused by the lack of an (exit) at the end, I think.

I think the search followed by read-line is going to print out the rest of the file, following the match. Is this what you want?

What problems are you encountering?

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

cormullion wrote: What problems are you encountering?
The same one(s) you are. I'm going see if sticking (exit) in there works.

I'd rather have one line returned, instead of the rest of the file, but that's just the way it works if I type the path and search strings by hand.

lemme go see what happens...

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

Here's an update:

Code: Select all

#!/usr/bin/newlisp

(set 'dir-path "/path/to/dir/")
(set 'all-args (join (rest (rest (main-args))) " "))
(set 'bbook (append dir-path ((parse all-args) 0)))
(set 'bverse ((parse all-args) 1))
(set 'bverse2 ((parse all-args) 2))
(set 'bverse3 ((parse all-args) 3))
;(set 'bverse4 ((parse all-args) 4))
;(set 'bverse5 ((parse all-args) 5))
(set 'bjoined (append bverse bverse2 bverse3))

(set 'file (open bbook "read"))
(search file bjoined)
(print (read-line file) "\n")
(close file)

(exit)
Ok. Adding (exit) eliminated the first error, but there are a couple more
problems. I want my script to spit out a bible verse from a directory
filled with one book per file, and one verse per line.

This works

Code: Select all

bash$ script.lsp Job 1:2
but the colon was counted as an argument by itself. I was able to
work around that.

this does not work

Code: Select all

bash$ script.lsp 2Sa 1:7
it balks on the leading "2"
Can I work around this too?

Lutz
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

You would have to put single quotes around the argument:

Code: Select all

bash$ script.lsp '2Sa' 1:7
or you could quote the whole argument line and parse it yourself in your program:

Code: Select all

bash$ 'script.lsp 2Sa 1:7'
Lutz

tom
Posts: 168
Joined: Wed Jul 14, 2004 10:32 pm

Post by tom »

Hi Lutz,

the single quotes around the argument did not work. I
got this error:

value expected in function search : file

I did parse out the arguments in my script, instead of
at the bash prompt, and it works without quoting anything.
Just in case anyone
wants to see the simple thing, here it is:

Code: Select all

#!/usr/bin/newlisp


(set 
	 'dir-path "/path/to/dir/"
	 'all-args (join (rest (rest (main-args))) " ")
	 'args-split (parse all-args " ")
	 'bbook-path (append dir-path (args-split 0))
	 'bbook (args-split 0)
	 'bverse (args-split 1)
	 )

(set 'file (open bbook-path "read"))
(search file bverse)
(print "\n" bbook " ")
(print (read-line file) "\n\n")
(close file)

(exit)

Locked