Connecting Tech Pros Worldwide Forums | Help | Site Map

How to search values in an array & if it matches data in a file, perform an action?

Newbie
 
Join Date: Feb 2008
Posts: 12
#1: May 6 '08
Question could also be asked, how to compare data between two arrays and perform an action (print cmd) everytime there is a match?

The problem I'm having with the code below is that the comparison in the conditional IF statement is only happening against the first value --$link-- in the array --@route--

I want the comparison between the two arrays to repeat for every value in the first array @route.

Appreciate any guidance or ideas you may be able to lend to get me on track.

Regards,

Hutch

P.S - not sure if the following is relevant... but here is a bit more context in case it helps guide your reply. The first Array --@route-- will contain 3 to 10 values every time the script is run. These values will most always be different from one time to the next.

Whereas the data in the CSV file that is being looped through, will most always not change and will contain 100 to 1000 fairly short lines of data.


Expand|Select|Wrap|Line Numbers
  1.  
  2. # open CSV file to Read From
  3. open(CSV_FILE, "/netmap/sites/list.csv") || 
  4. die "Can't open file: $!";
  5.  
  6. # turn each value in this array into a single object for purposes of matching against data in the CSV_FILE
  7. # **ARRAY 1**
  8. for $link (@route){
  9.  
  10. # Open the while loop to read csv file
  11. while (<CSV_FILE>) {
  12.  
  13. # Delete the new line char for each line
  14. chomp;
  15.  
  16. # Split each field in the CSV File into an array
  17. # **ARRAY 2**
  18. my @fields = split(/,/);
  19.  
  20.  
  21. # conditional statement to print a string of data everytime the while loop sees a match between values in the two arrays
  22.  
  23. if ($link eq "$fields[3]"){
  24.  
  25. print "test\n";
  26. print "we have a match for $fields[3]\n";
  27. }
  28. }
  29. }
  30.  
  31.  

numberwhun's Avatar
Site Moderator
 
Join Date: May 2007
Location: New Hampshire
Posts: 2,573
#2: May 6 '08

re: How to search values in an array & if it matches data in a file, perform an action?


You may want to take a read of any of a number of an online documents, like this one, which show you how to compare two arrays.

Regards,

Jeff
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#3: May 6 '08

re: How to search values in an array & if it matches data in a file, perform an action?


Quote:

Originally Posted by hutch75

Question could also be asked, how to compare data between two arrays and perform an action (print cmd) everytime there is a match?

The problem I'm having with the code below is that the comparison in the conditional IF statement is only happening against the first value --$link-- in the array --@route--

I want the comparison between the two arrays to repeat for every value in the first array @route.

Appreciate any guidance or ideas you may be able to lend to get me on track.

Regards,

Hutch

The comparison is happening only for first value in your array because, for the first iteration of @route array you are reading the file upto EOF. For the next element the execution will not enter into while (<CSV_FILE>) {} since it was already read till end.

You can change this behaviour by opening and closing the file within for $link (@route) { } block
Expand|Select|Wrap|Line Numbers
  1. for $link (@route){
  2.  open(CSV_FILE, "junk.txt") || 
  3. die "Can't open file: $!";
  4. # Open the while loop to read csv file
  5. while (<CSV_FILE>) {
  6.  
  7. # Delete the new line char for each line
  8. chomp;
  9.  
  10. # Split each field in the CSV File into an array
  11. # **ARRAY 2**
  12. my @fields = split(/,/);
  13.  
  14.  print $fields[3];
  15. # conditional statement to print a string of data everytime the while loop sees a match between values in the two arrays
  16.  
  17. if ($link eq "$fields[3]"){
  18.  
  19. print "test\n";
  20. print "we have a match for $fields[3]\n";
  21. }
  22. }
  23. close(CSV_FILE);
  24. }
  25.  
nithinpes's Avatar
Expert
 
Join Date: Dec 2007
Posts: 400
#4: May 6 '08

re: How to search values in an array & if it matches data in a file, perform an action?


Since, the file has 100-1000 lines, the better approach would be to read the file at once and store it into an array and later process elements of this array.
Expand|Select|Wrap|Line Numbers
  1. open(CSV_FILE, "junk.txt") || 
  2. die "Can't open file: $!";
  3. # Store csv file into an array
  4. @file=<CSV_FILE>;
  5. close(CSV_FILE); 
  6.  
  7.  
  8. for $link (@route){
  9.  
  10. # Delete the new line char for each line
  11. chomp;
  12. foreach(@file) {
  13.  
  14. my @fields = split(/,/);
  15.  
  16. if ($link eq "$fields[3]"){
  17.  print "test\n";
  18. print "we have a match for $fields[3]\n";
  19. }
  20. }
  21. }
  22.  
Reply