Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

split string at every character

Tags:

r

perl

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;
like image 713
user3741035 Avatar asked Sep 02 '25 10:09

user3741035


2 Answers

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.

like image 54
A5C1D2H2I1M1N2O1R2T1 Avatar answered Sep 03 '25 23:09

A5C1D2H2I1M1N2O1R2T1


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;
like image 36
hobbs Avatar answered Sep 04 '25 01:09

hobbs