Assembling small files in a large file but format is shifted

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DeepNik
    New Member
    • Feb 2010
    • 4

    Assembling small files in a large file but format is shifted

    Hi Perl Experts:
    I am relatively new to perl and did try to solve the problem by searching books and web but could not exactly solve it. Here is the problem. I want to gather "*.igf" files and put all the lines from these files in a larger file. However, each small file has a header on the first line. I want header only once in a bigger file. My current code does everything fine except content from file other than first file is shifted and it shifts the format. My question is how to discard first header line except when it is the first file but still maintain the format. Below is the code.

    Thank you for your help.
    DeepNik
    Code:
    my @igf_files = glob("*.igf");
    my $all_igf = "all_igf.igf";
    my $counter = 0;
    foreach my $fileigf (@igf_files) {
    if ($counter==0){
    open (IGF,"<$fileigf");
    	open (ALLIGF,"+>>$all_igf");
    	while(<IGF>)
    	{
    	 print ALLIGF $_;
    	}
    close (IGF  );
    close ( ALLIGF );
    $counter++;
    }
    else{
    open (IGF,"<$fileigf");
    my @lines =(<IGF>);
    open (ALLIGF,"+>>$all_igf");
    
    	{
    	print ALLIGF @lines[1..$#lines];
     	}
    close (IGF);
    close ( ALLIGF );
    }
    }
    Last edited by numberwhun; Feb 11 '10, 10:41 PM. Reason: Please use code tags!
  • RonB
    Recognized Expert Contributor
    • Jun 2009
    • 589

    #2
    Use the shift function to remove the first filename off of the array and assign it to a scalar.

    Process that file, then start your loop over the remaining files.

    Comment

    • DeepNik
      New Member
      • Feb 2010
      • 4

      #3
      Thank you Ron for your reply. However, yes, I can get the same functionality with shift operator but the formatting is still shifted. In the sense, content from the second small file is shifted by one space.

      DeepNik

      Comment

      • RonB
        Recognized Expert Contributor
        • Jun 2009
        • 589

        #4
        In what way is it shifted by 1 space?

        Do you end up with a single space between the contents of file1 and file2?
        Do you end up with a single space between between file2 and file3?
        Do you end up with a single space between each and every file?

        Or, does it shift the other way by stripping off 1 character?

        Can you attach a couple sample files so I can test and duplicate your issue?

        Comment

        • RonB
          Recognized Expert Contributor
          • Jun 2009
          • 589

          #5
          You should open the $all_igf file only once. Open it prior to opening the other files and keep it open until all files have been processed.

          Comment

          • DeepNik
            New Member
            • Feb 2010
            • 4

            #6
            Hi Ron:
            Thanks for sharing possibilities. It seems to me first, I should look into the way files were created. I create these files using a function and then process them. I am going to look into the way files are created. If I am unable to figure it out even after manipulating function, I will surely post sample files.

            Best,
            DeepNik

            Comment

            Working...