![]() |
| ||
| Can Array Name have Variable? 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; 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; 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- |
| ||
| Re: Can Array Name have Variable? 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. |
| ||
| Re: Can Array Name have Variable? Quote:
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);this is just a simple program to get common elements from two arrays. @arr = (1,2,3,4,5); hope this helps... katharnakh. |
| ||
| Re: Can Array Name have Variable? 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){
I tried a few things but kept getting the same error. A few are tried: . foreach (1..4){
orforeach (1..4){
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- |
| ||
| Re: Can Array Name have Variable? Use a hash of arrays: my %HoA = (); |
| ||
| Re: Can Array Name have Variable? Hi, consider following eg. $x = 10; Normally $$varindicates $varis a reference and ${$var} # same as $$var should return a scalar value pointed to by $var. Perl first checks whether $varis 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. Quote:
You can use hash of array data structure, as Kavin suggested in the last post, for your problem. katharnakh. |
| All times are GMT -4. The time now is 11:17 pm. |
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC