Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

I can't see how that's possible. The only thing you append to the file is a newline.

Here's a guess on what you're really looking for. You want to append the three files to the $fa file. You'll need to open each in turn, read from it, then print the contents to the $fa file (those variable names could really be more descriptive, by the way).

So I think what you want is something like this:

#!/usr/bin/perl use strict; use Data::Dumper; use File::Glob ':glob'; my @dirs = glob("C:/Documents and Settings/mydir/Desktop/KOMP/*"); foreach my $maid_dir (@dirs) { # Combined the first two conditionals to reduce indentation if (-d $maid_dir and $maid_dir=~m%\d+\s[A-Z]%){ # match the dir na +me my $seq_dir = $maid_dir."/sequencing/"; chdir ($maid_dir) or die "Can't chdir to $maid_dir: $!\n"; next if (-M $seq_dir > 1.0); chdir ($seq_dir) or die "Can't chdir to $seq_dir: $!\n"; #print Dumper ($maid_dir, $seq_dir); opendir my $dh, "$seq_dir" or die "Can't open $seq_dir: $!\n"; if(-d $seq_dir){ my @files = readdir $dh; #my $path = @_; for my $f(@files){ if($f=~ m%^(\d*)(HU.fa|HD.fa|Ltvec_small.FA|_fasta)$%) +{ my ($fa, $hu, $hd, $lt)= ($1."_fasta", $1."HU.fa", + $1."HD.fa", $1."Ltvec_small.FA"); if(-e $fa){ # This is output, not input. Renamed variable +accordingly. open( my $out, ">>".$seq_dir."/".$fa) or die " +Can't open $seq_dir/$fa for append: $!\n"; # Append the contents of the three files to $f +a open my $hu_file, '<', $hu or die "Can't open +$hu: $!\n"; while (my $line = <$hu_file>) { print $out $line; } open my $hd_file, '<', $hd or die "Can't open +$hd: $!\n"; while (my $line = <$hd_file>) { print $out $line; } open my $lt_file, '<', $lt or die "Can't open +$lt: $!\n"; while (my $line = <$lt_file>) { print $out $line; } #open( my $out, ">>".$seq_dir."/".$fa); print $out, "\n" ;#"$hu,$hd,$lt\n"; print Dumper($f); close($out); close($hu_file); close($hd_file); close($lt_file); } } } } } }

Note that this is a very quick revision of your code, with added error checks (and just noticed I missed the readdir, doh). And it may not be at all what you want. If not, please provide more detail on what you're trying to do.


In reply to Re^3: appending to a file by Nkuvu
in thread appending to a file by lomSpace

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others romping around the Monastery: (7)
    As of 2015-07-07 00:25 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









      Results (85 votes), past polls