Store in a 2 dimensional array - Perl | Unix Linux Forums | Shell Programming and Scripting

  Go Back    


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Store in a 2 dimensional array - Perl

Shell Programming and Scripting


Tags
perl

Closed Thread    
 
Thread Tools Search this Thread Display Modes
    #1  
Old 12-17-2012
@man @man is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 15 September 2014, 12:29 PM EDT
Location: Sweden
Posts: 59
Thanks: 25
Thanked 0 Times in 0 Posts
Store in a 2 dimensional array - Perl

Hey guyz.

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


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



Code:
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:


Code:
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:


Code:
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.
Sponsored Links
    #2  
Old 12-17-2012
bartus11's Avatar
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 12 September 2014, 1:32 PM EDT
Posts: 3,710
Thanks: 7
Thanked 1,142 Times in 1,113 Posts
Change print "$TF[$i]"; to print "$TF[$i]\t" . rand(1) . "\n"; .
The Following User Says Thank You to bartus11 For This Useful Post:
@man (12-17-2012)
Sponsored Links
    #3  
Old 12-17-2012
@man @man is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 15 September 2014, 12:29 PM EDT
Location: Sweden
Posts: 59
Thanks: 25
Thanked 0 Times in 0 Posts
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";



Code:
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? :/

Thanks alot
Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Store content from array to Spread_sheet using perl kavi.mogu Shell Programming and Scripting 0 01-02-2012 01:48 AM
PHP: Search Multi-Dimensional(nested) array and export values of currenly worked on array. zeekblack Shell Programming and Scripting 1 12-07-2009 09:30 PM
2 dimensional array in unix pritish.sas Shell Programming and Scripting 2 07-27-2009 10:53 PM
Help for record (2 dimensional array.) murtaza Shell Programming and Scripting 1 03-13-2007 08:10 PM
Reference two dimensional array in Perl sub photon Shell Programming and Scripting 5 04-16-2003 04:52 AM



All times are GMT -4. The time now is 11:36 PM.