Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

Re: doubt in storing a data of 2 lines in an array.

by shmem (Chancellor)
on Oct 30, 2006 at 15:10 UTC ( [id://581311]=note: print w/replies, xml ) Need Help??


in reply to doubt in storing a data of 2 lines in an array.

i have written a program to save each and every line in a seperate array. this is the program

which does not meet it's purpose, since you are saving all lines of your data file which don't begin with either ENTRY, TITLE, ORGANISM or ACCESSIONS into a single array which you name @se.

Let's look at your data file. It seems to be composed of multi-line records, in which each field begins on a separate line. Each field has an identifier up front (except the last record field which is just a sequence of chars with no blank in it), and some fields appear to be multi-line as well.

Since there is no record separator, you can only tell that all fields of a record are read when all field contents are read. Since your records appear to be ordered, I assume that is the case when that single-word line appears. All fields are stored in an anonymous array, which is pushed onto an array when done reading. After storing each record, a new anonymous array is initialized for the next record:

my $file = '/home/guest/sampir.txt'); open (PIR, '<', $file) or die "Can't read '$file': $!\n"; my @arr = (); my $se = []; # anonymous record array while(<PIR>) { chomp; if (/^(\w+)\s+/) # new field identifier, followed by blanks { push @$se, $_; } elsif (s/^\s+/ /) # if we can strip leading blanks, # it's a continuation line { $se->[-1] .= $_; # append to last field of this record } elsif(/^\w+$/) # must be the last field of the record { push @$se, $_; # save the last field push @arr, $se; # save the record array reference $se = []; # and make a new array reference for the next + record } else { die "Unknown line type at line $. of '$file'\n"; } }

Now you have all records in an array of arrays. See perldsc.

Your data structure now looks like
@arr = ( [ 'ENTRY CCHU #type complete', 'TITLE cytochrome c [validated] - human Homo sapiens', 'ORGANISM #formal_name Homo sapiens #common_name man', 'ACCESSIONS A31764; A05676; I55192; A00001', 'MGDVEKGKKIFIMKCSQCHTVEMGDVEKGGKHKTGPNLHGMIYARAJLFGRKTSEKGQAPGYSYT +AANKNKGIIWGEDTLMEYLENPKKYIP' ], [ 'ENTRY CCCZ #type complete', 'TITLE cytochrome c - chimpanzee (tentative sequence)', 'ORGANISM #formal_name Pan troglodytes #common_name chimpa +nzee', 'ACCESSIONS A00002', 'GDVEKGKKIFIMKCSQCHTSEKVEKGSSSKHKSSSTGPNLHGLMIYARAJFGRKTGSEKQAPGYS +YTAANKNKGIIWGED' ], [ 'ENTRY CCMQR #type complete', 'TITLE cytochrome c - rhesus macaque (tentative sequenc +e) Macaca mulatta ', 'ORGANISM #formal_name Macaca mulatta #common_name rhesus +macaque', 'ACCESSIONS A00003', 'GDVEKGKKIFIMKCSQSEKCHTVEKGGSSSSKHKTGPNLHGSSEKEMIYARAJKSEKLFGAAAAA +AAARKTGQAPGYSYTAANKSSSSNKGITWGEDTLMEYLENPKKYIPGTKMIFVGIKKKEE' ], [ 'ENTRY CCMKP #type complete', 'TITLE cytochrome c - spider monkey', 'ORGANISM #formal_name Ateles sp. #common_name spider monk +ey', 'ACCESSIONS A00004', 'GDVFKGKRIFIMKCSQCHTVESSSSKGGKHKTGPNLHGLMIYARAJSEKFGSSSSSSSSSSR' ] );

and the line

print $arr[0]->[1],"\n";

outputs

TITLE cytochrome c [validated] - human Homo sapiens

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^2: doubt in storing a data of 2 lines in an array.
by Anonymous Monk on Oct 31, 2006 at 10:20 UTC
    Thank you very much... i learnt how to do it, i can manage such problems myself later. thanks again

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://581311]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (3)
As of 2024-04-25 04:39 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found