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
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |