Store in a 2 dimensional array - Perl

Hey guyz.

Here is my sample input file following by first part of my code:

*	A	B	C	D	E
reg1	1	0	1	1	0
reg2	0	1	0	0	1
reg3	1	0	0	1	0
reg4	0	0	1	0	1
reg5	1	1	0	0	1
use strict; 
use warnings;

open (IN, "test_input.txt") or die ("Can't open file.txt: $!\n");
my $line = <IN>; 
chomp $line; 

my @TF = split ('\t', $line);
shift (@TF);

while ($line = <IN>) {
	my @temp = split('\t', $line);
	shift (@temp);
	my $i = 0;
	
	while ($i < @TF) {
		if ($temp[$i]==1) {
			print "$TF[$i]";
		}
		$i++;	
	}
}

close(IN);

It prints a list:

ACDBEADCEABE

I want this list to be stored in a 2 dimensional array following by a random number between 0-1 in second column. Desired array:

A	0.724392652
C	0.100361935
D	0.980176163
B	0.626905862
E	0.545560827
A	0.207170636
D	0.233475703
C	0.248689653
E	0.441124913
A	0.695127525
B	0.028040103
E	0.980644345

Any help? Thanks.

Change print "$TF[$i]"; to print "$TF[$i]\t" . rand(1) . "\n"; .

1 Like

Thanks bartus11!

now that I have this 2 dimensional array I would like to do this afterward:

I want to fill an empty matrix with these letters in this array. The reason for creating random numbers is to take the letters in a random order.

So I add another column to this array with 0 values:

print "$TF[$i]\t" . rand(1) . "\t0\n";

A	0.724392652	0
C	0.100361935	0
D	0.980176163	0
B	0.626905862	0
E	0.545560827	0
A	0.207170636	0
D	0.233475703	0
C	0.248689653	0
E	0.441124913	0
A	0.695127525	0
B	0.028040103	0
E	0.980644345	0

After sorting based on random numbers the letter is taken and feed to this empty matrix. Each row shouldn't have more than 3 letters in total. by taking each letter the 0 value in array is replaced by 1 , so next time if it is 1 means that we have to skip since it has been taken once.

Do you think you can help me with this? :confused:

Thanks alot