Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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}

In reply to Re: doubt in storing a data of 2 lines in an array. by shmem
in thread doubt in storing a data of 2 lines in an array. by heidi

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-03-29 14:18 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found