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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hutch75
    New Member
    • Feb 2008
    • 12

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

    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.


    [CODE=perl]

    # open CSV file to Read From
    open(CSV_FILE, "/netmap/sites/list.csv") ||
    die "Can't open file: $!";

    # turn each value in this array into a single object for purposes of matching against data in the CSV_FILE
    # **ARRAY 1**
    for $link (@route){

    # Open the while loop to read csv file
    while (<CSV_FILE>) {

    # Delete the new line char for each line
    chomp;

    # Split each field in the CSV File into an array
    # **ARRAY 2**
    my @fields = split(/,/);


    # conditional statement to print a string of data everytime the while loop sees a match between values in the two arrays

    if ($link eq "$fields[3]"){

    print "test\n";
    print "we have a match for $fields[3]\n";
    }
    }
    }

    [/CODE]
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    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

    Comment

    • nithinpes
      Recognized Expert Contributor
      • Dec 2007
      • 410

      #3
      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
      [CODE=perl]
      for $link (@route){
      open(CSV_FILE, "junk.txt") ||
      die "Can't open file: $!";
      # Open the while loop to read csv file
      while (<CSV_FILE>) {

      # Delete the new line char for each line
      chomp;

      # Split each field in the CSV File into an array
      # **ARRAY 2**
      my @fields = split(/,/);

      print $fields[3];
      # conditional statement to print a string of data everytime the while loop sees a match between values in the two arrays

      if ($link eq "$fields[3]"){

      print "test\n";
      print "we have a match for $fields[3]\n";
      }
      }
      close(CSV_FILE) ;
      }
      [/CODE]
      Last edited by nithinpes; May 6 '08, 04:06 AM. Reason: edited text

      Comment

      • nithinpes
        Recognized Expert Contributor
        • Dec 2007
        • 410

        #4
        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.
        [CODE=perl]
        open(CSV_FILE, "junk.txt") ||
        die "Can't open file: $!";
        # Store csv file into an array
        @file=<CSV_FILE >;
        close(CSV_FILE) ;


        for $link (@route){

        # Delete the new line char for each line
        chomp;
        foreach(@file) {

        my @fields = split(/,/);

        if ($link eq "$fields[3]"){
        print "test\n";
        print "we have a match for $fields[3]\n";
        }
        }
        }
        [/CODE]

        Comment

        Working...