•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the Perl section within the Software Development category of DaniWeb, a massive community of 374,193 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 3,560 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.
Please support our Perl advertiser:
Views: 946 | Replies: 5 | Solved
![]() |
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:
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:
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-
@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.
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.
Perl Syntax (Toggle Plain Text)
@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$_"}; }
Perl Syntax (Toggle Plain Text)
@arr = (1,2,3,4,5); @comp = (2,4,1,7,8); @common = grep {(join '', @comp) =~ /$_/} @arr; $, = ' '; print @common;
hope this helps...
katharnakh.
challenge the limits
Thanks katharnakh.
I am running into an issue with using the strict pragma. I 'Use strict' to start and define my arrays as:
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:
I tried a few things but kept getting the same error. A few are tried:
.
or
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-
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;
}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-
Last edited by godevars : Apr 17th, 2008 at 10:34 am.
Hi, consider following eg.
Normally
Perl first checks whether
strict pragma tells perl not to look for symbolic references and its also way to switchoff this facility.
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.
Perl Syntax (Toggle Plain Text)
$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.
...
You can use hash of array data structure, as Kavin suggested in the last post, for your problem.
katharnakh.
challenge the limits
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb Perl Marketplace
- Problem in Reading Array Variable (C)
- constructor with an array as an argument (C++)
- encapsulating an array in a class? (C#)
- Homework: filling array from text file (VB.NET)
- Array Values not found in Methods (Java)
- Resizing of an array. (C++)
- Array declaration problem (C++)
- How do I create a program using an Array ? (C++)
Other Threads in the Perl Forum
- Previous Thread: count characters in a string
- Next Thread: How to lock SDBM file when that file is writing by multiple programs at the same time


Linear Mode