User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 402,010 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,421 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.

Can Array Name have Variable?

Join Date: Mar 2008
Posts: 17
Reputation: godevars is an unknown quantity at this point 
Rep Power: 1
Solved Threads: 0
godevars's Avatar
godevars godevars is offline Offline
Newbie Poster

Can Array Name have Variable?

  #1  
Apr 16th, 2008
Here's one I haven't been able to find an answer to. I have 4 arrays:
@array1, @array2, @array3, @array4. I ran code to compare the values of 2 arrays to see number of values in common:

use strict;
use warnings;

my (%union, %intersect);
foreach my $e (@array1, @array2) {
    $union{$e}++ && $intersect{$e}++
}
my @intersect = sort keys %intersect;

print FILEOUT "@intersect\n";
print FILEOUT scalar @intersect;

I was thinking, if I wanted to compare all pairs of arrays and values they had in common, I could create a loop that started with @array1 and compared it to each array. In th loop I would make the digit in the array name a variable. For example:

use strict;
use warnings;

my (%union, %intersect, $i);

while ($i <6) {
    foreach my $e (@array1, @array$i) {
         $union{$e}++ && $intersect{$e}++
     }
     my @intersect = sort keys %intersect;
     print FILEOUT "@intersect\n";
     print FILEOUT scalar @intersect;
     $i++;
}

As you probably know already, the @array$i variable doesn't work.

In the end I'm looking to have a grid where I see belwo but was starting witht he basics:

array1 array2 array3 array4
array1 10 5 17 2
array2 5 15 8 1
array3 17 8 14 6
array4 2 1 6 19

I could create an outer loop that would have tha array names each witha variable in the foreach loop.
Thanks-
AddThis Social Bookmark Button
Reply With Quote  
All times are GMT -4. The time now is 9:45 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC