Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

how to split the elements in an array of arrays ?

by heidi (Sexton)
on Oct 31, 2006 at 18:26 UTC ( #581541=perlquestion: print w/ replies, xml ) Need Help??
heidi has asked for the wisdom of the Perl Monks concerning the following question:

hi all, i have a list of long stretch of strings in a file. i want to read the whole file and save it in an array where i can convert the elements into an array again, eg; my file has:
hellohellohellohellohellohello hihihihihihihihihihi byebyebyebyebyebyebyebye (etc........)
i want the hello line in an array, hi in an array and bye in another. same way for the rest of the lines in this file. i have done till that part using hashes. but what i am not able to do is, i want to split each and every letter in an array. eg: in the first line, i want to split up 'h' 'e' 'l' 'l' 'o' and so on til the end of the string. same for all the lines in the file...am not gettin it. am tryin my best. need ur guidance so that i can learn more. thanks.

Comment on how to split the elements in an array of arrays ?
Download Code
Re: how to split the elements in an array of arrays ?
by ptum (Priest) on Oct 31, 2006 at 18:28 UTC

    I'm sure lots of us would be glad to help. What have you tried so far, heidi?

    Hint: try split with an empty pattern //.

      this is the program which i have done (which is a part of the whole program where i am doin other processings too)
      open (PIR,'file.txt'); while (<PIR>) { chomp; if( /^ENTRY/ ) { $entry = $_ } elsif ( /^(TITLE)\s+(\S.*)/ ) { $title = "$1\n\t $2" } elsif ( /^(ORGANISM)\s+(\S.*)/ ) { $org = "$1\n\t $2" } elsif ( /^ACCESSIONS/ ) { $acc = $_ } else { push @se, $_; }
      this is my file: now plz dont confuse with the query file ok...let me explain u.
      ENTRY CCHU #type complete TITLE cytochrome c [validated] - human ORGANISM #formal_name Homo sapiens #common_name man ACCESSIONS A31764; A05676; I55192; A00001 MGDVEKGKKIFIMKCSQCHTVEKGGKHKTGPNLHGLFGRKTGQAPGYSYTAANKNKGIIWGEDTLMEYLE +NPKKYIPGTKMIFVGIKKKEERADLIAYLKKATNE ENTRY CCCZ #type complete TITLE cytochrome c - chimpanzee (tentative sequence) ORGANISM #formal_name Pan troglodytes #common_name chimpanzee ACCESSIONS A00002 GDVEKGKKIFIMKCSQCHTVEKGGKHKTGPNLHGLFGRKTGQAPGYSYTAANKNKGIIWGEDTLMEYLEN +PKKYIPGTKMIFVGIKKKEERADLIAYLKKATNE ENTRY CCMQR #type complete TITLE cytochrome c - rhesus macaque (tentative sequence) ORGANISM #formal_name Macaca mulatta #common_name rhesus macaq +ue ACCESSIONS A00003 GDVEKGKKIFIMKCSQCHTVEKGGKHKTGPNLHGLFGRKTGQAPGYSYTAANKNKGITWGEDTLMEYLEN +PKKYIPGTKMIFVGIKKKEERADLIAYLKKATNE
      @se will collect all the sequence in an array.. and then my query proceeds...instead of hello hi and bye, there will be the alphabetical sequences. i just wanted to get an idea and thats why i used hello hi and bye. hope u dont mind. waiting for ur reply. thanks.

        Perhaps I am not understanding you correctly, but this seems to be a very different question than the one you asked first. In the example you gave, what happens to the values in $entry, $title, $org or $acc? You seem to drop them on the floor ...

        To solve this kind of a problem, I would probably establish a hash of array references, something like this (untested):

        use strict; my %filecontents = ('entries' => [], 'titles' => [], 'organisms' => [], 'accessions' => [], 'unknown' => [] ); my $filehandle; unless (open($filehandle,"<file.txt")) { die "Cannot open file.txt: $!"; } while (<$filehandle>) { chomp; if (/^ENTRY/) { push @{$filecontents{'entries'}}, $_; } elsif (/^TITLE/) { push @{$filecontents{'titles'}}, $_; } # and so on ... }

        Update: Notice, by the way, that I check for success in opening the filehandle, always a good thing to do.

        Update 2: Arrghh! I was duped! I didn't realize you'd asked this question before. Why not pay attention to the answers you've already received, as Fletch points out below?

        Huzzah, the crufty re-implementation of Bio::SeqIO continues unabashed by suggestions to the contrary and pointers to its documentation.

        Addendum: That perhaps sounds a bit cruel, but if you don't understand enough Perl to know how to split scalars into characters you really shouldn't be trying to re-invent wheels that other people have already solved. Use the off-the-shelf widgets for the parts you can, and get fancy once you know what you're doing. Something like Beginning Perl for Bioinformatics that's aimed at biology-types specifically should help you get going.

Re: how to split the elements in an array of arrays ?
by wjw (Deacon) on Oct 31, 2006 at 20:11 UTC
    If I understand you correctly, the only line you are really interested in is the one with the long string of all_caps letters which you have stored as a scalar element in an array called @se. Instead of one big string, you want that to contain a reference to an array of individual letters?

    Have to agree with ptum, but for clarity's sake, the

    'unknown' => [];

    would get filled with

     'unknown' => {split(//,$_)};

    . This could be done with an array of arrays too though... and I would probably go with a hashes of hashes with the last value of each contained hash being the array of letters... But then maybe I am just dazed and confused... :-)

    ...the majority is always wrong, and always the last to know about it...

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://581541]
Approved by imp
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (10)
As of 2014-09-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (143 votes), past polls