By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,245 Members | 1,287 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,245 IT Pros & Developers. It's quick & easy.

comparing two arrays

P: 58
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...
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4.  
  5. opendir(DIR, "D:/data/eclipse/workspace/contents");
  6.  
  7. foreach my $MyDir (sort readdir(DIR))
  8.     {
  9.         print "$MyDir\n";
  10.     }
  11. 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
Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Text::CSV;
  5.  
  6. my $MyDir2 = "D:/data/eclipse/workspace/contents/navteq/01.csv";
  7. my $csv = Text::CSV->new();
  8. open (CSV, "<", $MyDir2) or die $!;
  9.     while (<CSV>) {
  10.         if ($csv->parse($_)) {
  11.             my @columns = $csv->fields();
  12.             print "$columns[0]\n";
  13.         } else {
  14.             my $err = $csv->error_input;
  15.             print "Failed to parse line: $err";
  16.         }
  17.     }
  18. close CSV;
  19.  
one problem here is that I can't get the array to sort tried this for lines 11 and 12
Expand|Select|Wrap|Line Numbers
  1. my @columns = $csv->fields();
  2. my @columns2 = sort $columns[0];
  3. 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..

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. use Text::CSV;
  5.  
  6. opendir(DIR, "D:/data/eclipse/workspace/contents/RijTijd_in");
  7.  
  8. foreach my $MyDir (sort readdir(DIR))
  9.     {
  10.         print "$MyDir\n";
  11.     }
  12. closedir (DIR);
  13.  
  14. my $MyDir2 = "D:/data/eclipse/workspace/contents/navteq/01.csv";
  15. my $csv = Text::CSV->new();
  16. open (CSV, "<", $MyDir2) or die $!;
  17.     while (<CSV>) {
  18.         if ($csv->parse($_)) {
  19.             my @columns = $csv->fields();
  20.             print "$columns[0]\n";
  21.         } else {
  22.             my $err = $csv->error_input;
  23.             print "Failed to parse line: $err";
  24.         }
  25.     }
  26. close CSV;
  27.  
  28. if (my $MyDir cmp my $MyDir2)
  29.     {print "it is the same\n";
  30. } else {
  31.     print "not the same\n"
  32. }
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
Apr 15 '08 #1
Share this Question
Share on Google+
3 Replies


P: 58
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
Apr 15 '08 #2

eWish
Expert 100+
P: 971
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.

Expand|Select|Wrap|Line Numbers
  1. opendir(my $DIR, $some_dir) || die "Can't open dir: $somedir: $!\n";
Also, only admin's and moderators are allowed to delete a post/thread.

--Kevin
Apr 15 '08 #3

P: 58
However, you should add the die function when you are opening directory's and files to make sure that nothing goes wrong.
Expand|Select|Wrap|Line Numbers
  1. opendir(my $DIR, $some_dir) || die "Can't open dir: $somedir: $!\n";
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

Expand|Select|Wrap|Line Numbers
  1. #!/usr/local/bin/perl
  2. use strict;
  3. use warnings;
  4. use File::Copy;
  5. my $pid;
  6. die "cant fork $!\n" unless defined($pid=fork());
  7.  if($pid) {
  8.  print "Here we go\n";
  9.  my $in=<STDIN>;
  10.    while ($in ne 'stop') {
  11.    $in=<STDIN>;
  12.    chomp $in;
  13.    print "$in is your input\n";
  14.        if($in eq 'stop') {
  15.        print "exiting.........\n";
  16.        }
  17.    }
  18.  }
  19.  else {
  20. LOOP1:
  21.   sleep 10;
  22.   {move_files()}
  23.  
  24. goto LOOP1;
  25.  
  26.  }
  27.  
  28. sub move_files {
  29. my $DirIn = "D:\\data\\eclipse\\workspace\\contents\\in";
  30. my $DirOut = "D:\\data\\eclipse\\workspace\\contents\\out";
  31. my $directory_count=0;
  32. my $file_count=0;
  33.  
  34. opendir(DIR, $DirIn) || die "Can't open DIR: $DirIn: $!\n";
  35. LINE: while(my $FILE = readdir(DIR)) {
  36. next LINE if($FILE =~ /^\.\.?/);
  37.  
  38. ## check to see if it is a directory
  39. if(-d "$FILE"){
  40. $directory_count++;
  41. }
  42. else {
  43. $file_count++;
  44. }
  45. }
  46. closedir(DIR);
  47.  
  48.  
  49. my $divisor = 9;
  50. my $remainder = $file_count % $divisor;
  51. my $FilesLeft = ($divisor - $remainder);
  52. 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
  53. my $newlocation = "D:\\data\\eclipse\\workspace\\contents\\out\\01.csv"; #hopefull I can just use the $DirOut variable here?
  54.  
  55.  
  56. 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"};
  57. }
  58.  
  59.  
  60. print "you can say bye bye now or \n";
  61. print "do something else here before you go\n";
  62. kill ("TERM",$pid); # not that graceful of an exit though 
  63.  
Apr 15 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.