Hallo, this script is thought to be a first step of building a file importing routine. But it is not (entirely) working. I have a list of IDs written into a text file. The script should scan a directory and search for alle the filenames, which contain the ID, listed in the text file. It works, if I define the array with the listed IDs directly into the script, but not, if I open and read the text file. I'm working with cygwin on WinXP
! /usr/local/bin/perl -w #Skript searches for filenames in a spec. directory #referring to a text file which contains a list of Ids $dir = $ARGV[0]; #Scan a directory and write filenames into an array opendir (SOURCE, $dir)or die "Cannot open dir: $!\n"; foreach $file (readdir SOURCE) { push (@dirFile, $file); #more than 6000 filenames } closedir SOURCE; #Open a filehandle and read a list from a text file $ref = "indexDS.txt"; open(FH, $ref) or die "Cannot open file: \n$!"; @ds = <FH>; close (FH); chomp @dirFile; chomp @ds; #Test: it works if array is defined like this: #@ds = ("I C 7702", "I C 7710", "I C 7713"); #Search for element of @ds as regex in @dirFile foreach $line (@ds) { @result = grep (/$line/, @dirFile); foreach (@result) { print "$_\n"; } }
In reply to Looking up elements of an array in another array! by better
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |