473,402 Members | 2,064 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

comparing two arrays

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
3 2427
erbrose
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
971 Expert 512MB
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
erbrose
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

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

Similar topics

11
by: Peter | last post by:
Hi how can I compare two byte arrays in VB.NET Thank Peter
1
by: Iain | last post by:
Hi Hopefully I am missing something really simple with this question, but here goes. I have two Bitarrays that I would like to compare. At the moment, I am XORing one with the other and...
12
by: Elijah Bailey | last post by:
I have two char arrays of size k. I want to know which one is bigger (exactly like for instance I compare two ints/longs/etc.). What is the fastest way to do this? k <= 10 usually for my...
4
by: agent349 | last post by:
First off, I know arrays can't be compared directly (ie: if (arrary1 == array2)). However, I've been trying to compare two arrays using pointers with no success. Basically, I want to take three...
1
by: Donald Grove | last post by:
If I have two arrays, what is a good paradigm for comparing what is in them, to determine what elements they share, or don't share? Specifically, each array could potentially contain the integers...
4
by: eoghan.kenny | last post by:
Hi, I need to compare two timestamp columns in sql server and see which one is greater. (i can tell if they are not equal buts not enough for this requirement). A timestamp value is unique in...
19
by: Ole Nielsby | last post by:
How does the GetHashCode() of an array object behave? Does it combine the GetHashCode() of its elements, or does it create a sync block for the object? I want to use readonly arrays as...
11
by: Sheldon | last post by:
Hi, I have two arrays that are identical and contain 1s and zeros. Only the ones are valid and I need to know where both arrays have ones in the same position. I thought logical_and would work...
1
by: psmahesh | last post by:
Hi folks, I am comparing two arrays and removing matches from the second array from the first array. Can someone take a look at this code below and mention if this is okay and perhaps if there...
12
by: filippo nanni | last post by:
Hello everybody, my question is this: I have two multidimensional arrays and I have to create a third one (for later use) from comparing these two. Here is my example code: //BEGIN CODE var...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.