Help reading a file

M

micky

Hi All,

I am reading a text file which has been formatted using spaces like this:

--------
node_2382 --> Peptostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
node_2384 --> coccostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
--------

And so on. I'd like to read it into an array such that node_xxxx goes in
char[1], the string after --> goes in @char[2], the number in the 1st
column goes into @char[3] and so on.

Could someone help me with this?

Thanks,
-M
 
G

Gunnar Hjalmarsson

micky said:
I am reading a text file which has been formatted using spaces like this:

--------
node_2382 --> Peptostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
node_2384 --> coccostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
--------

And so on. I'd like to read it into an array such that node_xxxx goes in
char[1], the string after --> goes in @char[2], the number in the 1st
column goes into @char[3] and so on.

What have you tried?

I see seven numbers in the 1st column.

What does the last "and so on" mean?
 
J

Josef Moellers

micky said:
Hi All,

I am reading a text file which has been formatted using spaces like this:

--------
node_2382 --> Peptostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
node_2384 --> coccostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
--------

And so on. I'd like to read it into an array such that node_xxxx goes in
char[1], the string after --> goes in @char[2], the number in the 1st
column goes into @char[3] and so on.

Could someone help me with this?

Maybe "perldoc -f substr" can do, as these seem to be all fixed-format
lines.
 
T

Tad McClellan

Subject: Help reading a file


You don't need help reading a file.

You need help processing the contents of a file.

[snip data]
And so on. I'd like to read it into an array such that node_xxxx goes in
char[1], the string after --> goes in @char[2], the number in the 1st
column goes into @char[3] and so on.

Could someone help me with this?


-----------------------------
#!/usr/bin/perl
use warnings;
use strict;

while ( <DATA> ) {
next unless /-->/;
my @fields = split / (?:--|==)> |\s+/;
foreach my $i ( 1 .. $#fields) {
print "$i: $fields[$i]\n";
}
print "\n";
}

__DATA__
node_2382 --> Peptostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
node_2384 --> coccostreptococc 10 1 0.333 T ==> C
21 1 0.500 G ==> A
23 1 0.500 G ==> A
25 1 0.750 G ==> T
31 1 1.000 C ==> T
45 1 0.500 G ==> A
47 1 0.250 T ==> C
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,204
Messages
2,571,063
Members
47,671
Latest member
peterweyand

Latest Threads

Top