in reply to Is it possible to write the results of a subroutine for data formatting to a text file?
There are a bunch of things that can be improved in your script. The solution to your immediate problem is to have the sub return the result string instead of printing it. But not too:
#!/usr/bin/perl use strict; use warnings; my $output = "Fmd.txt"; my $seq = "BATCATEATFATRAT"; my $result = splitSequence($seq, 3); print "\nResult:\n$result\n"; open my $outFile, '>', $output or die qq{Cannot open file "$output": $ +!.\n}; print $outFile "Formatted Data:\n$result\n\n"; close $outFile; sub splitSequence { my ($sequence, $length) = @_; my $result; while (length $sequence) { $result .= substr($sequence, 0, $length, '') . "\n"; } return $result; }
Prints:
Result: BAT CAT EAT FAT RAT
|
|---|