Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^3: appending to a file

by Nkuvu (Priest)
on Mar 19, 2009 at 17:38 UTC ( #751810=note: print w/replies, xml ) Need Help??


in reply to Re^2: appending to a file
in thread appending to a file

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.

Replies are listed 'Best First'.
Re^4: appending to a file
by lomSpace (Scribe) on Mar 19, 2009 at 21:56 UTC
    I want to append the three files to my $out file.
    I only need each file appended once. The loop is appending each file
    four times. Any ideas?

      Without a fair bit of rewriting your original code, you can add a last; after the final call to close() -- this will exit the for my $f(@files) loop.

      This isn't the way I'd write the script, personally, but I don't know enough about your file structure to revise it accurately. I'd probably use File::Find to make this a bit simpler.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://751810]
help
Chatterbox?
[usemodperl]: change the world? you mean BREAK THE WEB? we will regret this once google starts pulling certs for cursing or something...
Veltro Sees open mouth and feeds troll
[Corion]: usemodperl: Naah, you're just being unreasonable. It's not hard to download https URLs, and we've shown you ways to avoid downloading at all, and ways to keep downloading via http possible. You reject both.
[choroba]: Also, try asking at IRC
[usemodperl]: corion baby you have done nothing of the sort
[usemodperl]: asking how to download from cpan with core perl via http is unreasonable???
[Corion]: usemodperl: No, asking is not unreasonable. Insisting that it must always keep working for you is unreasonable.
[Corion]: But again, you haven't explained why your users need to download, or why they need to download via http, or why you can't host the SSL-stripper yourself.
[choroba]: Wait a second. Does shutting down search.cpan.org mean the cpan clients can no longer use their urllist?
[usemodperl]: i'm under no obligation to explain myself mommy, and i'm not insisting, just asking a simple question

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2018-06-24 16:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?



    Results (126 votes). Check out past polls.

    Notices?