Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

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

by 2teez (Priest)
on Mar 15, 2013 at 10:14 UTC ( #1023655=note: print w/ replies, xml ) Need Help??


in reply to Re^4: Looking up elements of an array in another array!
in thread Looking up elements of an array in another array!

Hi better,

The files, I am looking for, however do exist in the directory. So they should be found.

To start with Kenosis' script should work. I tried it, using your description of the problem you posted and I have outputs, though similar filenames which were not included in the "indexDS.txt" were also printed.

Secondly, how are you using the script, maybe you have some other code over-writing the variable "@result"
Hope this helps.

If you tell me, I'll forget.
If you show me, I'll remember.
if you involve me, I'll understand.
--- Author unknown to me


Comment on Re^5: Looking up elements of an array in another array!
Re^6: Looking up elements of an array in another array!
by better (Acolyte) on Mar 15, 2013 at 21:34 UTC

    Hi 2teez,

    thanks for checking! The positive results you made, made me wondering, why it wouldn't work for me. Starting point of my doubts was, that the matching of array elements worked, if I defined the @ds inside the srcipt. Could there something be wrong with the text file? I created the list of IDs by copying a coloumn out of an excel worksheet into a notepad file. After I created another text file with perl, Kenosis' script worked somehow. However, it printed the Id.numbers of the refering text file, not the matches of the directory scan.

    What I want to achieve is:

    1.) Look up the Ids given in a text file:

    I C 7700

    I C 7711

    etc..

    2. Scan a directory and return all filenames containing the IDS

    I C 7700 -A.jpg

    I C 7700 -B.jpg

    I C 7700 a,b -KK

    etc...

    And 3. (I haven't mentioned yet) Copy the matched files into another directory

    My original script with that text file created by perl did all three steps, but with the last element of the @ds only. Why the other files were not detected and copied, I don't know

    Ok. I will turn now to your next answer

    better

Re^6: Looking up elements of an array in another array!
by better (Acolyte) on Mar 17, 2013 at 17:08 UTC

    Hi,

    just to let you know too, it was the carriage return, connected to the IDs within the "bad" text file. After having removed all of them, parsing and matching was no prob anymore.

    Thanks again.

    How do I feel now?

    better ;-)

    And here is the code   -Sorry, but I couldn't use strict .-(

    #! /usr/local/bin/perl # #Script searches and copies large quantities of files # #Script reads a list of IDs given in a text file, #searches for filenames in a specified directory, # and copies all the files, which have the ID as a part of their name # into a new directory given that file exists in source dir #and does not exist in target dir! # #Results are reported into text file. # #Script doesn't work, if text file is created by parsing #a csv file and writing the IDs into the text file #Solution: Remove carriage return: $/="\r\n"; then: chomp # #tested: --ok! ############################################################### #use strict; use warnings; use File::Copy; #Input Source directory --ok! my $dir = $ARGV[0]; if ( !$ARGV[0] ) { print "Error: Missing input directory!\n"; } if ( !-d $ARGV[0] ) { print "Error: Input is no directory\n"; } #Open filehandle and parse list from a text file --ok! my $ref = "./data/indexIDs.txt"; #Important: Text file must not conta +in carriage returns!!! open(FH, '<', $ref) or die "Cannot open file: \n$!"; my @ids = <FH>; chomp @ids; #print "$_\n" for @ids; # tested --ok! close (FH); #test target directory --ok! my $dirTarget = "/cygdrive/d/bildimport/"; if( !-d $dirTarget) { print "Can not open $dirTarget: $!\n"; } #Scan a directory and write filenames into an array --ok! opendir (SOURCE, $dir)or die "Cannot open dir: $!\n"; foreach my $file (readdir SOURCE) { push (@dirFile, $file); chomp @dirFile; } closedir SOURCE; #Report results of copying process into a text file # 3 possible options: #file copied; #file not copied: file exists; #file not copied: file not found open OUT, '>./data/results.txt' or die "Cannot open file: $!\n"; ###################################################################### +### ###MAIN #Use element of @ds as regex and search for all matches in @dirFile - +-ok! #Copy file, if filename matches foreach my $line (@ids) { if (grep /$line/, @dirFile) { my @result = grep (/$line/, @dirFile); foreach (@result) { #print OUT "$_\n"; #tested: --ok! #Build path to files in source dir and to tar +get directory my $res = $dir.$_, "\n"; my $tar = $dirTarget.$_, "\n"; #Start copy! if (!-e $tar) { copy ($res, $tar) or die "Cannot copy: + $!"; print OUT "$_ ;;;copied!\n"; } else { print OUT "$_ ;;;not copied: file exis +ts!\n"; } } } else { print OUT "$line ;;;not copied: file not found!\n"; } } close OUT;

      Hi again,

      when I announced the solution of all my problems was found, I was to hasty.

      I got a problem with perl matching greedy. See my reply to teez2

      better

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1023655]
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-10-25 13:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    For retirement, I am banking on:










    Results (143 votes), past polls