[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Pattern extraction and Wildcard expansion



>  I have the function:
> 
>  nl = {
>  }
>  fn s pat {
>          lis = `{for (dir = $path) ls $dir}
> 	 a = <={~~ $lis $pat}
> 	 echo $a^$nl
>  }
> 
>  and try to execute:
> 
>  	; s l*
> 
>  Mi intention is to get a list of commands that begin with "l", but
>  there is no output.
> 
>  Where is my error?

Quoting.  ~ and ~~ are special with regard to quoting.  The pattern
argument to ~ and ~~, if it includes wildcards, must be an unquoted
literal.  Eval might be helpful here, but introduces at least as many
problems as it solves.

I tend to do that sort of thing with ``here's the prefix I'm interested
in'' explicitly, though you can make eval work properly, with care.

There are a couple of other problems.  The l* argument to s will be
expanded against the list of files in the current directory before s is
called, so you're matching againt the wrong thing;  if l* doesn't match
anything in the current directory, you'll get the equivalent of 'l*'.

--p

Here's a crude toy, based on the thing you were writing:

    fn commands-starting-with prefixes {
	for (dir = $path) {
	    for (file = $dir/$prefixes^*) {
		if { access -x $file } {
		    echo $file
		}
	    }
	}
    }

    ; commands-starting-with ghost java
    /home/jdk/bin/java
    /home/jdk/bin/java-rmi.cgi
    /home/jdk/bin/javac
    /home/jdk/bin/javadoc
    /home/jdk/bin/javah
    /home/jdk/bin/javap
    /usr/bin/ghostscript
    /usr/bin/X11/ghostview
    /usr/X11R6/bin/ghostview
    ;

I think it should be reasonable to do something like:

    commands-matching '*view'

by using eval, but that's left as an exercise for the reader.

--p