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-

There are a lot of array handling modules that do this kind of work already. But you could use a hash of arrays and give the keys any value you wanted that would associate it with the respective array.

You could also use the grep() function because arrays can be processed super fast, and you can compare many arrays for various conditions very rapidly with grep.

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

No, you can actually do that. This is just a simple program to print a set of arrays, having some common in array identifiers.

@a1 = (2,3,4);
@a2 = (8,4,5,7,8);
@a3 = (1,3,2,6);

# print "\n",join '', @{"a$_"} foreach (1..3); # this is aggregated form
foreach (1..3){
	print "\n",join '', @{"a$_"};
}

this is just a simple program to get common elements from two arrays.

@arr = (1,2,3,4,5); 
@comp = (2,4,1,7,8);
@common = grep {(join '', @comp) =~ /$_/} @arr;
$, = ' ';
print @common;

hope this helps...
katharnakh.

Thanks katharnakh.
I am running into an issue with using the strict pragma. I 'Use strict' to start and define my arrays as:

my (@array1, @array2, @array3, @array4);

I am using strict since this is only a portion of my code.
The error I receive is 'Global symbol at "@array1" requires explicit package name.' I have:

foreach (1..4){
	push @{"a$_"}, $value;
}

I tried a few things but kept getting the same error. A few are tried:
.

foreach (1..4){
	push my(@{"a$_"}), $value;
}

or

foreach (1..4){
                my (@array1, @array2, @array3, @array4);
	push @{"a$_"}, $value;
}

I wanted to keep the strict pragma on. I did turn it off and it worked. Will that give me other issues down the road? I know when posting code, 'use strict' and 'use warnings' is important.
Thanks-

Thanks again-

Use a hash of arrays:

my %HoA = ();
for (1..4) {
   push @{$HoA{$_}},$value;
}

Hi, consider following eg.

$x = 10;
$var = "x";
$$var = 30; # Modifies $x to 30 , because $var is a symbolic reference !

Normally $$var indicates $var is a reference and ${$var} # same as $$var should return a scalar value pointed to by $var .
Perl first checks whether $var is a reference? which is not(in the above case): it is a string, then perl treats $var 's content as an identifier. This is how symbolic references work and it is important to note that it works only on global variables not on private/lexical variables marked by my .

strict pragma tells perl not to look for symbolic references and its also way to switchoff this facility.

...
I wanted to keep the strict pragma on. I did turn it off and it worked. Will that give me other issues down the road? I know when posting code, 'use strict' and 'use warnings' is important.
...

It is a good programming practice use strict and warning pragmas. I guess above eg. explains why did perl throw run time error.
You can use hash of array data structure, as Kavin suggested in the last post, for your problem.

katharnakh.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.