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 423,734 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,356 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: Programming Forums
Views: 1371 | Replies: 5 | Solved
Reply
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  
Join Date: Mar 2006
Posts: 619
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 33
KevinADC's Avatar
KevinADC KevinADC is online now Online
Practically a Master Poster

Re: Can Array Name have Variable?

  #2  
Apr 16th, 2008
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.
Reply With Quote  
Join Date: Jan 2006
Posts: 221
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 20
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Can Array Name have Variable?

  #3  
Apr 17th, 2008
Originally Posted by godevars View Post
...
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.
  1. @a1 = (2,3,4);
  2. @a2 = (8,4,5,7,8);
  3. @a3 = (1,3,2,6);
  4.  
  5. # print "\n",join '', @{"a$_"} foreach (1..3); # this is aggregated form
  6. foreach (1..3){
  7. print "\n",join '', @{"a$_"};
  8. }
this is just a simple program to get common elements from two arrays.
  1. @arr = (1,2,3,4,5);
  2. @comp = (2,4,1,7,8);
  3. @common = grep {(join '', @comp) =~ /$_/} @arr;
  4. $, = ' ';
  5. print @common;

hope this helps...
katharnakh.
challenge the limits
Reply With Quote  
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

Re: Can Array Name have Variable?

  #4  
Apr 17th, 2008
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-
Last edited by godevars : Apr 17th, 2008 at 10:34 am.
Reply With Quote  
Join Date: Mar 2006
Posts: 619
Reputation: KevinADC is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 33
KevinADC's Avatar
KevinADC KevinADC is online now Online
Practically a Master Poster

Re: Can Array Name have Variable?

  #5  
Apr 17th, 2008
Use a hash of arrays:

my %HoA = ();
for (1..4) {
   push @{$HoA{$_}},$value;
}
Reply With Quote  
Join Date: Jan 2006
Posts: 221
Reputation: katharnakh is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 20
katharnakh's Avatar
katharnakh katharnakh is offline Offline
Posting Whiz in Training

Re: Can Array Name have Variable?

  #6  
Apr 17th, 2008
Hi, consider following eg.
  1. $x = 10;
  2. $var = "x";
  3. $$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.

Originally Posted by godevars View Post
...
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.
challenge the limits
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb Perl Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the Perl Forum

All times are GMT -4. The time now is 1:22 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC