Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Re: Looking up elements of an array in another array!

by Kenosis (Priest)
on Mar 14, 2013 at 22:36 UTC ( #1023570=note: print w/ replies, xml ) Need Help??


in reply to Looking up elements of an array in another array!

Here's another option:

use strict; use warnings; my $dir = shift; my $IDs = join '|', map { /(.+)/; "\Q$1\E" } <>; my @results = grep /$IDs/, <$dir/*>; print "$_\n" for @results;

Usage: perl script.pl '/the/dir/to/scan' indexDS.txt [>outFile]

The target directory is (implicitly) shifted off @ARGV and saved for later. The first <> notation reads the index file. map takes each line from the file, and the regex in it matches all characters except the newline. The captured line is surrounded by \Q ... \E to quote any meta-characters in the ID. The results, e.g., "\Q$1\E", are joined with the alternation symbol |, effectively creating an "or" type regex that's used in the grep. A file glob's used to read the directory files and only those names which contain one of the IDs are passed to @results.

Hope this helps!


Comment on Re: Looking up elements of an array in another array!
Select or Download Code
Re^2: Looking up elements of an array in another array!
by better (Acolyte) on Mar 14, 2013 at 23:38 UTC

    Hello Kenosis,

    thanks a lot for your help. Unfortunately it doesn't help. The last entry (>outFile) causes an error. But if I skip that, nothing happens. (But the Ids given in indexDS.txt are matching filenames of the scanned directory) Any suggestions?

    better

      replace the [>outFile] with >outFile ... this is redirecting stdout to the file outFile in the current directory.

      kielstirling ++ is right on target, and my apologies for not explaining the [>fileOut] parameter. This designates an optional argument that's used to direct output to a file, instead of the screen. So, for example, you could do the following on the command line:

      perl script.pl '/the/dir/to/scan' indexDS.txt >results.txt

      Thus, instead of printing to the screen, output's directed to a file. Omitting the last parameter will just print the results to the screen.

        Thanks again, Kenosis. Yes, indeed. Entering the third option without brackets, does create a text file, but without any results(?) The files, I am looking for, however do exist in the directory. So they should be found.

        Hmm.. If I get the two arrays (@ds and @dirFile) in my script listed, I can see that grep /$line/ should find the matching elements in the second array. But @results keeps beeing empty. The funny thing is: if I define @ds writing some of the wanted IDs directly into the script

         @ds = ("I C 7702", "I C 7710");

        the script works perfectly. But I want the array filled by reading a long list of IDs from a text file. Any idea what is going wrong?

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023570]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (7)
As of 2014-12-21 14:47 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (106 votes), past polls