Feral_Akodon has asked for the wisdom of the Perl Monks concerning the following question:
I split a tab-delimited file into multiple files based on the value in column one. I modified another person's code and can't figure out how to give the output files custom names. Now I'm trying to rename the files (which all have different names) based on the contents of a tab-delimited text file (again all of the new names are unique).
I know the code to do a single rename but I have been unable to get the correct syntax to rename all files based on the contents of the txt document. I have 736 output files and will likely have to do this in the future with different files, so while I could rename one at a time, it seems like I'm better off in the longrun if I can rename all of the output files using the txt file.
I could also fix the problem with the original split script if I could customize the output names (I could get rid of the intermediate rename script all together). In the program I can modify the output names, but if I change one part of the name (%03d), then I get errors about the file not being clobbered.
Here is the split script.
# config: my $field = 0; my $sep = "\t"; $, = $sep; $\ = $/; my %file; # { ID, sequence, $fh } my $fID = 1; while (<INFILE>) { chomp; my @c = split /$sep/o; my( $key, $ID ) = defined $c[$field] ? ( $c[$field], $fID++ ) : ( '(column not present)', 0 ); unless ( $file{$key} ) { $file{$key}{ID} = $ID; $file{$key}{sequence} = sprintf '%03d.tabular', $file{$key}{ID +}; -f $file{$key}{sequence} and die "Sorry, '$file{$key}{ID}' exists; won't clobber."; open $file{$key}{fh}, ">", $file{$key}{sequence} or die "Error opening '$file{$key}{sequence}' for write - $!"; } print {$file{$key}{fh}} @c; } print OUTFILE $file{$_}{sequence}, $_ for sort { $file{$a}{ID} <=> $file{$b}{ID} } keys %file;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Renaming Multiple Files with Different Names
by NetWallah (Canon) on Nov 02, 2011 at 02:30 UTC | |
by Feral_Akodon (Initiate) on Nov 02, 2011 at 15:41 UTC | |
by Feral_Akodon (Initiate) on Nov 02, 2011 at 16:18 UTC | |
by choroba (Cardinal) on Nov 02, 2011 at 16:29 UTC | |
|
Re: Renaming Multiple Files with Different Names
by graff (Chancellor) on Nov 02, 2011 at 07:22 UTC | |
by Feral_Akodon (Initiate) on Nov 02, 2011 at 16:10 UTC | |
by NetWallah (Canon) on Nov 02, 2011 at 17:10 UTC | |
by Feral_Akodon (Initiate) on Nov 02, 2011 at 17:30 UTC |