First, you use just s/\r?\n//;, but perhaps you meant $line =~ s/\r?\n//;

You've been given excellent feedback. I agree with aaron_baugher that a hash of arrays of arrays (HoAoA) is likely a good fit for your program. With that in mind, consider the following:

use Modern::Perl; use Data::Dumper; my %HoAoArrays; while (<DATA>) { chomp; my ( $col0, @cols1_3 ) = split /\t/; push @{ $HoAoArrays{$col0} }, \@cols1_3; } for my $key ( keys %HoAoArrays ) { my $numElements= @{ $HoAoArrays{$key} }; for ( my $i = 0 ; $i < $numElements; $i++ ) { say '${ $HoAoArrays{' . $key . '} }[' . $i . ']->[1] = ' . ${ $HoAoArrays{$key} }[$i]->[1]; } say ''; } say '', Dumper \%HoAoArrays; __DATA__ KN-1791-LAST_rep_c7834 IMGA|Medtr4g125100.1 2e-139 497 KN-1791-LAST_rep_c7834 IMGA|Medtr4g125100.1 4e-46 187 KN-1791-LAST_rep_c7834 IMGA|Medtr4g125100.2 4e-46 187 KN-1792-LAST_rep_c7834 IMGA|Medtr4g125100.1 2e-150 497 KN-1792-LAST_rep_c7834 IMGA|Medtr4g125100.1 4e-37 187 KN-1792-LAST_rep_c7834 IMGA|Medtr4g125100.2 4e-37 187 KN-1792-LAST_rep_c7834 IMGA|Medtr4g125100.3 4e-35 188

Output:

${ $HoAoArrays{KN-1792-LAST_rep_c7834} }[0]->[1] = 2e-150 ${ $HoAoArrays{KN-1792-LAST_rep_c7834} }[1]->[1] = 4e-37 ${ $HoAoArrays{KN-1792-LAST_rep_c7834} }[2]->[1] = 4e-37 ${ $HoAoArrays{KN-1792-LAST_rep_c7834} }[3]->[1] = 4e-35 ${ $HoAoArrays{KN-1791-LAST_rep_c7834} }[0]->[1] = 2e-139 ${ $HoAoArrays{KN-1791-LAST_rep_c7834} }[1]->[1] = 4e-46 ${ $HoAoArrays{KN-1791-LAST_rep_c7834} }[2]->[1] = 4e-46 $VAR1 = { 'KN-1792-LAST_rep_c7834' => [ [ 'IMGA|Medtr4g125100.1', '2e-150', '497' ], [ 'IMGA|Medtr4g125100.1', '4e-37', '187' ], [ 'IMGA|Medtr4g125100.2', '4e-37', '187' ], [ 'IMGA|Medtr4g125100.3', '4e-35', '188' ] ], 'KN-1791-LAST_rep_c7834' => [ [ 'IMGA|Medtr4g125100.1', '2e-139', '497' ], [ 'IMGA|Medtr4g125100.1', '4e-46', '187' ], [ 'IMGA|Medtr4g125100.2', '4e-46', '187' ] ] };

The while loop splits each tab-delimited line into a scalar ($col0) and an array (@cols1_3). The @{} notation enclosing the hash (i.e., @{ $HoAoArrays{$col0} }) tells perl to treat it as an array, and an array reference is pushed onto it.

The subsequent for loop shows how to deference the array references. Finally, Dumper is used to visually represent the created data structure: { } = hash and [ ] = array. Dumper shows a hash with two keys. Each key has a corresponding array as its value, and each array has arrays as its elements (HoAoA).

Hope this helps!


In reply to Re: Accessing 2D array values and comparing by Kenosis
in thread Accessing 2D array values and comparing by mSe

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.