zsh-users
 help / color / mirror / code / Atom feed
* check for existence without full globbing
@ 2003-02-03 21:24 Le Wang
  2003-02-04  2:49 ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Le Wang @ 2003-02-03 21:24 UTC (permalink / raw)
  To: Zsh users list

Hi,

I wonder if there is a way to check if a glob pattern can be matched without
getting all files that can be globbed, i.e. a function that return true after
the first match is found.

--
Le

______________________________________________________________________ 
Post your free ad now! http://personals.yahoo.ca


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: check for existence without full globbing
  2003-02-03 21:24 check for existence without full globbing Le Wang
@ 2003-02-04  2:49 ` Bart Schaefer
  2003-02-04  3:29   ` Le Wang
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2003-02-04  2:49 UTC (permalink / raw)
  To: Zsh users list

On Feb 3,  4:24pm, Le Wang wrote:
} 
} I wonder if there is a way to check if a glob pattern can be matched
} without getting all files that can be globbed, i.e. a function that
} return true after the first match is found.

Out of curiosity, why?

It can be done, but it requires forking a subshell, which may not make
it any faster than just generating the list of files to begin with:

if ( : *(Ne:exit 0:) ; exit 1 )
then
  print There are some files in this directory
else
  print There are no files in this directory
fi


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: check for existence without full globbing
  2003-02-04  2:49 ` Bart Schaefer
@ 2003-02-04  3:29   ` Le Wang
  2003-02-04  5:16     ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: Le Wang @ 2003-02-04  3:29 UTC (permalink / raw)
  To: Zsh users list

 --- Bart Schaefer <schaefer@brasslantern.com> wrote: > On Feb 3,  4:24pm, Le
Wang wrote:
> } 
> } I wonder if there is a way to check if a glob pattern can be matched
> } without getting all files that can be globbed, i.e. a function that
> } return true after the first match is found.
> 
> Out of curiosity, why?

I need to get a list of directories that have .java files in them.  Here is
the snipplet:

for i ( $sourcePath ) {
  for j ( $i/**/*(-/) ) {
    setopt localoptions
    setopt nullglob
    unsetopt globsubst
    tempArr=( $j/*.java )

    if (( $#tempArr >= 1 )); then
      temp=${j#$i/}
      temp=${temp//\\//.}
      packages=( $packages $temp )
    fi
  }
}

It was taking quite a while to glob through all the .java files.  But it was
even slower to use the subshell method (like you suspected).  Ohh well.

--
Le

______________________________________________________________________ 
Post your free ad now! http://personals.yahoo.ca


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: check for existence without full globbing
  2003-02-04  3:29   ` Le Wang
@ 2003-02-04  5:16     ` Bart Schaefer
  2003-02-04  9:38       ` John Buttery
  0 siblings, 1 reply; 6+ messages in thread
From: Bart Schaefer @ 2003-02-04  5:16 UTC (permalink / raw)
  To: Zsh users list

On Feb 3, 10:29pm, Le Wang wrote:
}
} I need to get a list of directories that have .java files in them.  Here is
} the snipplet:
} 
} for i ( $sourcePath ) {
}   for j ( $i/**/*(-/) ) {

The (-) qualifier there probably doesn't do what you think.  **/ won't
descend through symlinks, so **/*(-/) will not descend beyond the first
symlink in any path.  Hence you'll only find the .java files that are
directly within the symlinked directories, not those in any subdirs of
the symlinked directories, if you see what I mean.

If you can be sure the symlinks don't cycle, you can use ***/ instead.

}     setopt localoptions
}     setopt nullglob
}     unsetopt globsubst
}     tempArr=( $j/*.java )
} 
}     if (( $#tempArr >= 1 )); then
}       temp=${j#$i/}
}       temp=${temp//\\//.}
}       packages=( $packages $temp )
}     fi
}   }
} }
} 
} It was taking quite a while to glob through all the .java files.

I'll bet it's taking much longer to do the ${j#$/} ${temp//\\//.} stuff,
plus packages=( $packages $temp ), in a loop, than to do the globbing.
And you should almost never put setopt/unsetopt commands inside a loop
body, because they don't revert at the end of the loop (only at the end
of a shell function), so all you're doing every time around is resetting
something that's already set.

Try this and see if it's faster:

    typeset -U packages
    packages=( ${~~^sourcePath}/./**/*(N-/) )
    packages=( ${~~^packages}/**/*.java(N:h) )
    packages=( ${${~~packages##*/./}:gs,/,.} )

The double tildes in ${~~...} turn off globsubst for that expansion, and
the N turns on nullglob, so you don't have to mess around with setopt.

-- 
Bart Schaefer                                 Brass Lantern Enterprises
http://www.well.com/user/barts              http://www.brasslantern.com

Zsh: http://www.zsh.org | PHPerl Project: http://phperl.sourceforge.net   


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: check for existence without full globbing
  2003-02-04  5:16     ` Bart Schaefer
@ 2003-02-04  9:38       ` John Buttery
  2003-02-04 18:01         ` Bart Schaefer
  0 siblings, 1 reply; 6+ messages in thread
From: John Buttery @ 2003-02-04  9:38 UTC (permalink / raw)
  To: Zsh users list

[-- Attachment #1: Type: text/plain, Size: 706 bytes --]

* Bart Schaefer <schaefer@brasslantern.com> [2003-02-04 05:16:23 +0000]:
[snip Bart's extremely well-informed, as usual, response]

  Just out of curiosity, isn't this a problem that's neatly solved with
a utility like find(1)?  I realize that zsh is more than qualified to
perform this task, but don't we have dedicated binaries to do things for
a reason?  This isn't a rhetorical question; I'm sure there is an
answer, I'm just wondering what it is... 

-- 
------------------------------------------------------------------------
 John Buttery
                                     (Web page temporarily unavailable)
------------------------------------------------------------------------

[-- Attachment #2: Type: application/pgp-signature, Size: 189 bytes --]

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: check for existence without full globbing
  2003-02-04  9:38       ` John Buttery
@ 2003-02-04 18:01         ` Bart Schaefer
  0 siblings, 0 replies; 6+ messages in thread
From: Bart Schaefer @ 2003-02-04 18:01 UTC (permalink / raw)
  To: Zsh users list

On Feb 4,  3:38am, John Buttery wrote:
} 
}   Just out of curiosity, isn't this a problem that's neatly solved with
} a utility like find(1)?

In this particular instance, I don't know of any way to get find to
descend all directories while reporting the name of each directory that
contains a plain file matching a certain pattern.  I'd be happy to be
proved wrong ... but my zsh solution isn't doing that either.

However, in the general case the best answer is: Unless efficiency is an
overriding concern, use whatever utility you're more familiar with.

To use any external utility like `find', you have to fork it and read
its output. That's quite a bit of overhead. If you're going to pipe
the output off to some other external program, or use the result as
arguments to another program, then `find' is fine. If you want to suck
the output back into zsh to do string manipulations, it's probably more
efficient to use a glob in an array assignment.


^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2003-02-04 18:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-03 21:24 check for existence without full globbing Le Wang
2003-02-04  2:49 ` Bart Schaefer
2003-02-04  3:29   ` Le Wang
2003-02-04  5:16     ` Bart Schaefer
2003-02-04  9:38       ` John Buttery
2003-02-04 18:01         ` Bart Schaefer

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/zsh/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).