Thanks Anomalous, I see what you mean by backsliding- I had been testing the code with a single file to control for errors with the loop (and avoid being overwhelmed by error messages). Here is the updated code that works. I see other responses suggesting better ways to do this and potential problems with the hash, and I will address those as well. But at the moment, this works. Thanks for all your help!

#!/usr/bin/perl use strict; use warnings; my %hash = (); open my $fh, '<', 'sample_name_ID_hash.txt' or die "Cant open file $!" +; LINE: while (my $line = <$fh>) { my ($key, $value) = split /\s/, $line; next LINE if not $key; $hash{$key} = $value; chomp (%hash); } close $fh; @file_array = <*.nex> or die $!; #find all the files foreach $file (@file_array) { my $prefix = $file; $prefix =~ s/\.nex//; open FH, "$file", or die $!; print "found $file with prefix $prefix\n"; open OUT, ">out$prefix.nex" or die $!; while (<FH>) { if (/^\s+\'(\S+):(\S\d+)\|\w+\|(\d)\S+\'(.*)\n/) { $var1 = "$1"; $var2 = "$2"; $var3 = "$3"; $var4 = "$4"; print OUT "'$hash{$var1}_${var2}_$var3'$var4\n"; } elsif (/(.*)\n/) { print OUT "$1\n"; } } }

In reply to Re^4: In place search and replace with a hash by hkates
in thread In place search and replace with a hash by hkates

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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, details, 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, summary, 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.