Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

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

by better (Acolyte)
on Mar 17, 2013 at 17:08 UTC ( #1023926=note: print w/ replies, xml ) Need Help??


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

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;


Comment on Re^6: Looking up elements of an array in another array!
Download Code
Re^7: Looking up elements of an array in another array!
by better (Acolyte) on Mar 23, 2013 at 06:40 UTC

    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://1023926]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others about the Monastery: (4)
As of 2014-11-21 03:34 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My preferred Perl binaries come from:














    Results (104 votes), past polls