Unix/Linux Go Back    


Shell Programming and Scripting Unix shell scripting - KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and shell scripts and shell scripting languages here.

Store in a 2 dimensional array - Perl

Shell Programming and Scripting


Tags
perl

Closed Linux or Unix Question    
 
Thread Tools Search this Thread Display Modes
    #1  
Old Unix and Linux 12-17-2012
@man @man is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 20 October 2014, 10:22 AM 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 Unix and Linux 12-17-2012
bartus11's Unix or Linux Image
bartus11 bartus11 is offline Forum Staff  
Moderator
 
Join Date: Apr 2009
Last Activity: 28 June 2015, 6:17 PM EDT
Posts: 3,733
Thanks: 7
Thanked 1,152 Times in 1,122 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 Unix and Linux 12-17-2012
@man @man is offline
Registered User
 
Join Date: Jul 2012
Last Activity: 20 October 2014, 10:22 AM 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 Linux or Unix Question

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Linux 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 10:20 PM.