in reply to I'm trying to print the contents of a hash to newly created files

If you want multiple files shouldnt the open/close be inside the foreach loop?

And  my $filename = "'$id'"; is going to try to open a file with single quotes in its name, i kinda doubt you want that.

  • Comment on Re: I'm trying to print the contents of a hash to newly created files
  • Download Code

Replies are listed 'Best First'.
Re^2: I'm trying to print the contents of a hash to newly created files
by Peter Keystrokes (Beadle) on May 08, 2017 at 18:22 UTC
    Thank you, sir. I've incorporated your advice into my code in conjunction with advice about how I open my file handle and it seems to be working. Here's how my code looks now:
    my %id2seq = (); my $id = ''; open File,"human_hg19_circRNAs_putative_spliced_sequence.fa",or die $! +; while(<File>){ chomp; if($_ =~ /^>(.+)/){ $id = $1; }else{ $id2seq{$id} .= $_; } } foreach $id (keys %id2seq){ open my $out_fh, '>>', $id or die $!; ##Amendment here print $out_fh ($id."\n",$id2seq{$id}, "\n"); close $out_fh; ## moved into the foreach loop } close File;