inhd has asked for the wisdom of the Perl Monks concerning the following question:

I have the following two arrays:
@array1 = ("Member1\tMember2"......"Member1\tMember2);<br> @array2 = (\@Member1, \@Member2, \@state_living, \@Membership_status, +\@member_since);

@array1 is has two elements(Member1 and Member2 separated by tab). @array2 is a multidimensional array and has reference to arrays of Member1 and Member2, state_living, membership_status, and member since).

Member 1 and Member 2 are grouped together, and each one is a unique pair, but belong to different states (state_living). Number of states in states_living array could vary from 2 to n numbers.

I need to retrieve for each of the Member1 and Member2 combination present in @array1 , their state living, and membership status from the other @array2.

I want the output to look something like this:

Mem_status State_living_1 State_living_2 Year_Since active Member 1 Member 2 2001

I hope I am clear. Any help/suggestions on how I can compare the two different arrays here and get the required information for each array1 from array2?

Thanks

Replies are listed 'Best First'.
Re: how to compare elements of two multi-dimensional arrays
by GrandFather (Saint) on Oct 05, 2009 at 00:51 UTC

    Show us some code. You probably ought not be using arrays. Where does the data actually come from? If you are slurping the data from a file into your arrays, then don't! If you are retrieving the data from a database then you can probably do it in a way such that it is easier to deal with.

    In any case you have an inappropriate data format and if you don't show us the big picture we can't help sort it out for you.


    True laziness is hard work
Re: how to compare elements of two multi-dimensional arrays
by Sewi (Friar) on Oct 05, 2009 at 07:07 UTC
    I didn't completly understand your data structure, but I'm trying to give you some advice:
    The most basic way to merge two data structures is walking through the first, item by item, and for each item go through the second searching for matches:
    for my $a (@array1) { for my $b (@array2) { next if $a ne $b; # Do whatever tests you need here # Do the work here } }
    Sometimes index, grep and others could be used to replace the inner loop.
    Another idea would be indexing your second array by converting it into a hash. Now you could easily access each item when walking through the first array.

      Your last idea is by far the best solution. In fact the order in which you present the different techniques is exactly the progression taken by most people learning Perl (the trade off tends to be different for other languages). The equivalent of your nested loop using hashes is:

      for my $element (@array) { next if ! exists $lookup{$element}; # Do the work here }

      Although grep code often looks nice and clean (or at least compact), it is a nested loop and has the same trouble with performance when the arrays are large as the explicit nested loop you presented as a first try.

      btw, even for sample code don't use $a and $b - they are special variables used by sort. Unless you are golfing or writing obfuscated code avoid bed habits, especially when trying to teach someone else good habits. ;)


      True laziness is hard work