Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

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

by better (Acolyte)
on Mar 15, 2013 at 06:49 UTC ( [id://1023625]=note: print w/replies, xml ) Need Help??


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

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?

Replies are listed 'Best First'.
Re^5: Looking up elements of an array in another array!
by 2teez (Vicar) on Mar 15, 2013 at 10:14 UTC

    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

      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

      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

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (5)
As of 2024-03-29 21:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found