I want to split every character in a string and output it as comma or tab separated characters:
I need to use file_in
and file_out
since I have very many lines.
input
TTTGGC
TTTG
TGCAATGG
....
....
output
T,T,T,G,G,C
T,T,T,G
T,G,C,A,A,T,G,G
I have used this, but it prints every character vertically:
/usr/bin/perl
use strict;
use warnings;
my $data = 'Becky Alcorn';
my @values = split(undef,$data);
foreach my $val (@values) {
print "$val\n";
}
exit 0;
In R, you can use strsplit
and paste
:
Strings <- c("TTTGGC","TTTG","TGCAATGG")
vapply(strsplit(Strings, ""), function(x) paste(x, collapse=","), character(1L))
# [1] "T,T,T,G,G,C" "T,T,T,G" "T,G,C,A,A,T,G,G"
You can write the output using writeLines
, specifying sep = "\n"
if required.
Your code uses a loop to print the values of @values
one per line, so the computer does what you told it to. Try:
print join ",", @values;
or even condense your code all the way down to:
print join ",", split //, $data;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With