comparing two arrays

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • erbrose
    New Member
    • Oct 2006
    • 58

    comparing two arrays

    Hello all! Newbie here, I've been tasked with a fairly intensive project here and my perl skills are still at a minimum so this post may eventually turn into a long one, but I am only going to ask the immediate problems I am having first and try to figure the rest out myself.
    Basically I need to compare the contents of a directory, with the contents of a series of 'csv' files, when those two match it will trigger some moving of the contents of said directory to another one. But first here is the issue I am having.
    I can print the contents of the directory like this...
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    
    opendir(DIR, "D:/data/eclipse/workspace/contents");
    
    foreach my $MyDir (sort readdir(DIR))
    	{
    		print "$MyDir\n";
    	}
    closedir (DIR);
    Although its adds a . and .. to the array, thats fine I just added them to my csv files. I can also print the contents of a csv with this
    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Text::CSV;
    
    my $MyDir2 = "D:/data/eclipse/workspace/contents/navteq/01.csv";
    my $csv = Text::CSV->new();
    open (CSV, "<", $MyDir2) or die $!;
        while (<CSV>) {
            if ($csv->parse($_)) {
                my @columns = $csv->fields();
                print "$columns[0]\n";
            } else {
                my $err = $csv->error_input;
                print "Failed to parse line: $err";
            }
        }
    close CSV;
    one problem here is that I can't get the array to sort tried this for lines 11 and 12
    Code:
    my @columns = $csv->fields();
    my @columns2 = sort $columns[0];
    print "$columns2[0];
    but that does work either... help here would also be much appreciated.

    finally I piece the code together and try and do a cmp with my two arrays, but I am obviously taking everything out of contents am lost. here is what the whole mess looks like pieced together..

    Code:
    #!/usr/bin/perl
    use strict;
    use warnings;
    use Text::CSV;
    
    opendir(DIR, "D:/data/eclipse/workspace/contents/RijTijd_in");
    
    foreach my $MyDir (sort readdir(DIR))
    	{
    		print "$MyDir\n";
    	}
    closedir (DIR);
    
    my $MyDir2 = "D:/data/eclipse/workspace/contents/navteq/01.csv";
    my $csv = Text::CSV->new();
    open (CSV, "<", $MyDir2) or die $!;
        while (<CSV>) {
            if ($csv->parse($_)) {
                my @columns = $csv->fields();
                print "$columns[0]\n";
            } else {
                my $err = $csv->error_input;
                print "Failed to parse line: $err";
            }
        }
    close CSV;
    
    if (my $MyDir cmp my $MyDir2)
    	{print "it is the same\n";
    } else {
    	print "not the same\n"
    }
    I get warnings of "Use of uninitialized value $MyDir in string comparison (cmp) at line 28" and "Use of uninitialized value $MyDir2 in string comparison (cmp) at line 28"
    and "not the same" prints, not sure if its cause the second list in not sorted correctly or its just not reading them right.
    Any assistance will greatly be appreciated!
    Thanks,
    Eric
  • erbrose
    New Member
    • Oct 2006
    • 58

    #2
    Alright, sorry folks, I am attempting another route, so please ignor this last post.. it won't let me in to delete it and not sure why.
    Eric

    Comment

    • eWish
      Recognized Expert Contributor
      • Jul 2007
      • 973

      #3
      Since you are changing your approach I won't get into any details. However, you should add the die function when you are opening directory's and files to make sure that nothing goes wrong.

      [code=perl]opendir(my $DIR, $some_dir) || die "Can't open dir: $somedir: $!\n";[/code]
      Also, only admin's and moderators are allowed to delete a post/thread.

      --Kevin

      Comment

      • erbrose
        New Member
        • Oct 2006
        • 58

        #4
        Originally posted by eWish
        However, you should add the die function when you are opening directory's and files to make sure that nothing goes wrong.
        [code=perl]opendir(my $DIR, $some_dir) || die "Can't open dir: $somedir: $!\n";[/code]
        Also, only admin's and moderators are allowed to delete a post/thread.
        --Kevin
        Thanks Kevin! After browsing google for the last few hours, borrowing snippets of code here and there, I've pieced together most of what I want to do and it works, well almost!
        Here is the task in a nut shell. We have a directory with a bunch of csv files. Sorted correctly, I need to move the first 3 .csv files into another directory once a certain criteria is met. The criteria is that there is a number of files in another directory that is divisible by the number 9 with a remainder of 0. (automated swapping of files on an FTP server we load 3 files, they upload 9, repeat..)
        I've got the timer function down, and I can move a HARDCODED file into the right directory when the criteria is met, now the problem I am having is how to just get the first 3 files..
        here is my code so far with the HARDCODE file 01.csv that needs to be changed to the first 3 .csv files
        PLEASE excuse my lack of proper indenting and excess amount of code too

        Code:
        #!/usr/local/bin/perl
        use strict;
        use warnings;
        use File::Copy;
        my $pid;
        die "cant fork $!\n" unless defined($pid=fork());
         if($pid) {
         print "Here we go\n";
         my $in=<STDIN>;
           while ($in ne 'stop') {
           $in=<STDIN>;
           chomp $in;
           print "$in is your input\n";
               if($in eq 'stop') {
               print "exiting.........\n";
               }
           }
         }
         else {
        LOOP1:
          sleep 10;
          {move_files()}
        
        goto LOOP1;
          
         }
         
        sub move_files {
        my $DirIn = "D:\\data\\eclipse\\workspace\\contents\\in";
        my $DirOut = "D:\\data\\eclipse\\workspace\\contents\\out";
        my $directory_count=0;
        my $file_count=0;
        
        opendir(DIR, $DirIn) || die "Can't open DIR: $DirIn: $!\n";
        LINE: while(my $FILE = readdir(DIR)) {
        next LINE if($FILE =~ /^\.\.?/);
        
        ## check to see if it is a directory
        if(-d "$FILE"){
        $directory_count++;
        }
        else {
        $file_count++;
        }
        }
        closedir(DIR);
        
        
        my $divisor = 9;
        my $remainder = $file_count % $divisor;
        my $FilesLeft = ($divisor - $remainder);
        my $oldlocation = "D:\\data\\eclipse\\workspace\\contents\\in\\01.csv"; #this is where i need to somehow change to the first 3 files from the in directory
        my $newlocation = "D:\\data\\eclipse\\workspace\\contents\\out\\01.csv"; #hopefull I can just use the $DirOut variable here?
        
        
        if ($remainder==0){move ($oldlocation, $newlocation) or die "failed to move"; print "it worked it worked we moved it\n\n";} else {print "They are currently uploading files or an error has occurred there are ".$FilesLeft." files left to upload don't move em\n"};
        }
        
        
        print "you can say bye bye now or \n";
        print "do something else here before you go\n";
        kill ("TERM",$pid); # not that graceful of an exit though

        Comment

        Working...