Reading a large file into Perl array of arrays and manipulating the output for different purposes

Posted by Brian D. on Stack Overflow See other posts from Stack Overflow or by Brian D.
Published on 2010-06-10T16:06:40Z Indexed on 2010/06/10 16:32 UTC
Read the original article Hit count: 326

Filed under:
|
|
|

Hello,

I am relatively new to Perl and have only used it for converting small files into different formats and feeding data between programs.

Now, I need to step it up a little. I have a file of DNA data that is 5,905 lines long, with 32 fields per line. The fields are not delimited by anything and vary in length within the line, but each field is the same size on all 5905 lines.

I need each line fed into a separate array from the file, and each field within the line stored as its own variable. I am having no problems storing one line, but I am having difficulties storing each line successively through the entire file.

This is how I separate the first line of the full array into individual variables:

my $SampleID = substr("@HorseArray", 0, 7);

my $PopulationID = substr("@HorseArray", 9, 4);

my $Allele1A = substr("@HorseArray", 14, 3);

my $Allele1B = substr("@HorseArray", 17, 3);

my $Allele2A = substr("@HorseArray", 21, 3);

my $Allele2B = substr("@HorseArray", 24, 3);

...etc.

My issues are: 1) I need to store each of the 5905 lines as a separate array. 2) I need to be able to reference each line based on the sample ID, or a group of lines based on population ID and sort them.

I can sort and manipulate the data fine once it is defined in variables, I am just having trouble constructing a multidimensional array with each of these fields so I can reference each line at will. Any help or direction is much appreciated. I've poured over the Q&A sections on here, but have not found the answer to my questions yet.

Thanks!! -Brian

© Stack Overflow or respective owner

Related posts about perl

Related posts about arrays