in reply to subdirectory question

Your approach to the problem, as well as the problem description raises too many questions to provide a complete answer. Anyway , here is an attempt:

    ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

Replies are listed 'Best First'.
Re^2: subdirectory question
by ambrus (Abbot) on Jan 03, 2005 at 19:45 UTC

    Why are you using a hash to store what seems like a simple list of file names? I would use a simple @array.

    use the perl grep function to search an array.

    Don't you see the contradiction in these two sentences? I'd use a hash, but with the names read into the keys, not the values. That way, you can easily check whether a filename is in the hash. For example,

    while(<>) { /(.*)/; $names{$1} = 1; } use File::Find (); File::Find::find sub { $names{$_} and -x and print $File::Find::name, "\n"; }, grep -e, split ":", $ENV{"PATH"};
    searches for binaries with the names given on ARGV.
      Clever code!

      I would have used chomp instead of "/(.*)/", and split on ";" for Windoze.

      Anyway, to respond to your question - I had not optimized my proposed solution to the extent you have, so I did not see the "contradiction". My proposal suggested storing the file names in an array, and "grep"ping it for each file.

      I would agree that your solution is faster. To optimize memory it even more, you could set "$names{$1}=undef", and use "exists $names{$_}" when querying.

          ..."I don't know what the facts are but somebody's certainly going to sit down with him and find out what he knows that they may not know, and make sure he knows what they know that he may not know, and that's a good thing. I think it's a very constructive exchange," --Donald Rumsfeld

        Originally I thought of a hash because both posts (reading in a text file and subdirectory question) ask for a hash. If this was indeed some kind of homework, then whoever thought it out knew he can use a hash.

        You are right: chomp is actually better, because if you have newlines in filenames, you can give the program a nul-delimited list of filenames, start the script with perl -0, and it would magically work.

        I don't think that $names{$_} = (); would be better on memory usage than $names{$_} = 1;, as the former has to allocate an undef, which is not any smaller than an integer. Don't trust me in this however, as I'm not good in perlguts.