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

merging multiple files

Expand Messages
  • mustang97_gt
    Hi all, I m having trouble with merging multiple files together. Here is a small example of what I m trying to do: file1.dat file2.dat file3.dat 12345
    Message 1 of 1 , Aug 1, 2002
    • 0 Attachment
      Hi all,

      I'm having trouble with merging multiple files together.
      Here is a small example of what I'm trying to do:

      file1.dat file2.dat file3.dat
      12345 abcde ABCDE
      67890 fghij FGHIJ
      12345 klmno KLMNO
      67890 pqrst PQRST
      12345 uvwxy UVWXY

      Given an order list (2, 3, 1) and a size number like 1,
      I'd like to get an output file that looks like

      dataout.dat
      aA1bB2cC3dD4eE5
      fF6gG7hH8iI9jJ0
      ...
      ..


      The code I have now is as follows:
      #!/usr/local/bin/perl5

      @order = (2,3,1);
      $size = 1;

      #place the file in the proper order
      for($i=0; $i<@order ;$i++) {
      $number = $order[$i];
      $filename[$i] = "file".$number.".dat";
      # printf("file $i = $filename[$i]\n");
      }

      open(OUTPUTFILE, ">dataout.dat");
      #I'd like to loop until EOF but don't know how
      for ($j=0;$j<10; $j++) {
      for($i=0; $i<@order ;$i++) {
      $data = "";
      open(INPUTFILE, $filename[$i]);
      $offset = $size*$j;
      read(INPUTFILE, $data, $size, $offset);
      $data =~ s/\n//g;
      print(STDOUT "data = $data \n");
      print(OUTPUTFILE "$data");
      close(INPUTFILE);
      }
      }
      close(OUTPUTFILE);


      The output that I get for this case though is
      dataout.dat
      aA1aA1aA1aA1aA1aA1aA1aA1aA1aA1

      Can anyone tell me what errors I've made in the code?
      Am I not reading the file properly? It looks like
      the offset is not working to me. If anyone has any suggestions
      on how to accomplish this task, perhaps in an easier way,
      I'd greatly appreciate it.

      Thanks in advance,
      another perl newbie
    Your message has been successfully submitted and would be delivered to recipients shortly.