Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?

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 '/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!

Replies are listed 'Best First'.
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?


      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 '/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?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023570]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (8)
As of 2018-06-19 09:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.