CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
lines to columns

 



muluwork
Novice

Apr 21, 2012, 4:32 AM

Post #1 of 7 (2764 views)
lines to columns Can't Post

Hi there, I want to change lines to columns. What is the easiest way to do so? Your help is much appreciated. Here is my input data and the output shall look like (for clear view please see the attachment).

INPUT
target: gi|906065|gb|EB04006.1|EB04006
length: 211
miRNA : mir-103-2//mir-103//mir-103-1
length: 23
mfe: -33.2 kcal/mol
p-value: 0.000025
position 54
target 5' A UA G 3'
AUAGUCUUGUGCG GCUGCU
UAUCGGGACAUGU CGACGA
miRNA 3' AG UA 5'
target: gi|9060853|gb|EB03694.1|EB03694
length: 46
miRNA : mir-103-2//mir-103//mir-103-1
length: 23
mfe: -26.0 kcal/mol
p-value: 0.000071
position 5
target 5' U C A 3'
UGGCCU GUAUGAUGC
AUCGGG CAUGUUACG
miRNA 3' AGU A ACGA 5'



OUTPUT
gi|906065|gb|EB04006.1|EB04006 211 mir-103-2//mir-103//mir-103-1 23 -33.2 kcal/mol 0.000025 54
target 5' A UA G 3'
AUAGUCUUGUGCG GCUGCU
UAUCGGGACAUGU CGACGA
miRNA 3' AG UA 5'

gi|9060853|gb|EB03694.1|EB03694 46 mir-103-2//mir-103//mir-103-1 23 -26.0 kcal/mol 0.000071 5
target 5' U C A 3'
UGGCCU GUAUGAUGC
AUCGGG CAUGUUACG
miRNA 3' AGU A ACGA 5'
Attachments: data (1.19 KB)


wickedxter
User

Apr 21, 2012, 6:33 PM

Post #2 of 7 (2595 views)
Re: [muluwork] lines to columns [In reply to] Can't Post

here is my attempt.... this will work for the data u supplied for the INPUT thats above my post if the input will exactly like the attached file then let me know and Ill have to change the regex to match....

Code
use strict; 
use 5.014;


$/ = '';

my @data = map { split(/target:(.*?)target:/s)} <DATA>;

my $count=0;
for (@data){
$count++;
$_ =~ s/\n|\t//g;
$_ =~ /(.*)length: \d+ miRNA : (.*?) length: (\d+) mfe: (.*?) (target \d'.*?') (.*?) (miRNA .*)/;
say "$count $1 $2 $3 $4";
say $5;
say $6;
say $7;
say "\n";
}


__DATA__
target: gi|906065|gb|EB04006.1|EB04006
length: 211
miRNA : mir-103-2//mir-103//mir-103-1
length: 23
mfe: -33.2 kcal/mol
p-value: 0.000025
position 54
target 5' A UA G 3'
AUAGUCUUGUGCG GCUGCU
UAUCGGGACAUGU CGACGA
miRNA 3' AG UA 5'
target: gi|9060853|gb|EB03694.1|EB03694
length: 46
miRNA : mir-103-2//mir-103//mir-103-1
length: 23
mfe: -26.0 kcal/mol
p-value: 0.000071
position 5
target 5' U C A 3'
UGGCCU GUAUGAUGC
AUCGGG CAUGUUACG
miRNA 3' AGU A ACGA 5'



muluwork
Novice

Apr 22, 2012, 2:00 AM

Post #3 of 7 (2577 views)
Re: [wickedxter] lines to columns [In reply to] Can't Post

Thanks wickedxter, the data looks like exactly the attachment also my perl version is 5.12.4


wickedxter
User

Apr 22, 2012, 11:58 AM

Post #4 of 7 (2548 views)
Re: [muluwork] lines to columns [In reply to] Can't Post


Code
 
#change
use v5.014
#to
use v5.012
#or
use features qw(say);



muluwork
Novice

Apr 23, 2012, 8:16 AM

Post #5 of 7 (2521 views)
Re: [wickedxter] lines to columns [In reply to] Can't Post

hi wickedxter thanks. It is just print numbers 1, 2 and 3. I tried to understand it but not close to the root cause.
use strict;
use 5.012;


$/ = '';
open IN, shift;
INPUT:

my @data = map { split(/target:(.*?)target:/s)} <IN>;

my $count=0;
for (@data){
$count++;
$_ =~ s/\n|\t//g;
$_ =~ /(.*)length: \d+ miRNA : (.*?) length: (\d+) mfe: (.*?) (target \d'.*?') (.*?) (miRNA .*)/;
say "$count $1 $2 $3 $4";
say $5;
say $6;
say $7;
say "\n";
}


wickedxter
User

Apr 25, 2012, 4:17 AM

Post #6 of 7 (2492 views)
Re: [muluwork] lines to columns [In reply to] Can't Post

Are u inputing from STDIN? Or are u supplying a dir/to/file.txt?
Its only printing numbers because the regex isnt matching... Are u useing the file u supplied from ur orginal post? If so then the regex will have to change to match the spacing and what not. Because the current regex matches the input as u posted in ur message.


muluwork
Novice

Apr 28, 2012, 3:47 AM

Post #7 of 7 (2311 views)
Re: [wickedxter] lines to columns [In reply to] Can't Post

Yes, the input is from STDIN.

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives