Loading ...
Sorry, an error occurred while loading the content.

how do I do a recursive loop?

Expand Messages
  • mroman85308
    I have two csv files with 8 pieces of data per line. I want to see if a case ID number in the second list exists in the first list. Basically, I want to read
    Message 1 of 3 , Jan 2, 2004
    • 0 Attachment
      I have two csv files with 8 pieces of data per line.

      I want to see if a case ID number in the second list exists in the
      first list. Basically, I want to read in one line of the first file,
      then compare it against all of the lines of the second, then read in
      the second line of the first file and compare it against all the
      lines in the second, etc etc.


      Is there a certain controll structure I should use?

      Also, does anyone know the syntax of a join function?


      thanks for any help.

      marc
    • William Martell
      NOT TESTED!!! #!/perl -w use strict; $file1 = file1.csv ; $file2 = file2.csv ; open (FILE1, $file1) || can t open $file1 file. $! ; open (FILE2, $file2) ||
      Message 2 of 3 , Jan 2, 2004
      • 0 Attachment
        NOT TESTED!!!
         
        #!/perl -w
         
        use strict;
         
        $file1 = "file1.csv";
        $file2 = "file2.csv";
         
        open (FILE1, $file1) || "can't open $file1 file. $!";
         
        open (FILE2, $file2) || "can't open $file2 file. $!";
         
        while(<FILE1>){
          chomp;
          @datainfile1 = $_;
            while(<FILE2){
               foreach $data (@datainfile1){
                      if($data == $_){
                            print "Matched data found:  $data, $_ \n";
                         }
                    }
                 }
             }
         
         
        I think this has some problems but, it is the best I can do for now.  Comments or hints from more advanced programmers are welcome.
         
        Thanks
        Will Martell
        Dallas Tx
          
        ----- Original Message -----
        Sent: Friday, January 02, 2004 11:55 AM
        Subject: [beginning_perl] how do I do a recursive loop?

        I have two csv files with 8 pieces of data per line.

        I want to see if a case ID number in the second list exists in the
        first list. Basically, I want to read in one line of the first file,
        then compare it against all of the lines of the second, then read in
        the second line of the first file and compare it against all the
        lines in the second, etc etc.


        Is there a certain controll structure I should use?

        Also, does anyone know the syntax of a join function?


        thanks for any help.

        marc




        Yahoo! Groups Links

      • WilliamGunther@aol.com
        ######### #!/path/to/perl use strict; my (@match, @first, @second) = []; my ($first, $second, $x, $y) = 0; open(FIRSTLIST, listone.csv ) || die Couldn t
        Message 3 of 3 , Jan 2, 2004
        • 0 Attachment
          #########
          #!/path/to/perl
          use strict;
          my (@match, @first, @second) = [];
          my ($first, $second, $x, $y) = 0;
          open(FIRSTLIST, "listone.csv")  || die "Couldn't open File: $!";
          open(SECONDLIST, "listtwo.csv") || die "Couldn't open File: $!";
           
          @first = <FIRSTLIST>; @second = <SECONDLIST>;
          foreach $first (@first) {
                  foreach $second (@second) {
                      push(@match, [$x, $y]) if $first eq $second;
                      $y++
                  }
                  $x++
          }
          #########
           
          Syntax is clear, but untested. If you wanted to seperate the CSV and just compare ID numbers just add a couple splits. Matched data is stored in @match multidimensional array. If the first match, for example, is the first data #2, and second #3 then $match[0] is a reference to the anonymous array (2, 3)
           
          As to join, Perl manpages say:
           
          join EXPR,LIST
          Joins the separate strings of LIST into a single string with fields separated by the value of EXPR, and returns that new string. Example:
              $rec = join(':', $login,$passwd,$uid,$gid,$gcos,$home,$shell);

          Beware that unlike split, join doesn't take a pattern as its first argument.

           
          ----------------------------------------------------------------------------------------------
          William Gunther
          ----------------------------------------------------------------------------------------------
        Your message has been successfully submitted and would be delivered to recipients shortly.