Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations wOOdy-Soft on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Finding Unique Instances of Array in a 2D array

Status
Not open for further replies.

sloppyhack

Technical User
Apr 17, 2001
111
US
Does anyone know if it's possible to determine the number of unique instances of each array in a 2D array? If I could just get an array containing only one instance of each array I think I can do it. Anyone have any insight here? Cheers,

Sloppyhack
 
Hi there sloppy,

Hope this is what you need,

push @array, "first value";
push @array, "first value";
push @array, "second value";
push @array, "first value";

map { $hash{$_} = '' } @array;

foreach $key (keys %hash){
print "$key\n";
}

This creates an array with duplicate values and then de-duplicates it - is this what you meant?
Mike
________________________________________________________________

"Experience is the comb that Nature gives us, after we are bald."

Is that a haiku?
I never could get the hang
of writing those things.
 
Now how do you do that with arrays instead of values? If I have an array of arrays, things get a little funky. I am accessing the elements of the array with the following format:

$arrayname[(row number)][(element number)]

I know how to dedup a 1D array, but I have no clue if it's even possible to dedup an array of arrays. It would be really cool if you could.

Thanks for the help though.


Cheers,

Sloppyhack
 
Do you want to check that the elements of your array of arrays are references to the true same array or that they are arrays containing the same elements.
Code:
@array1 = (1, 2, 3);
@array2 = (1, 2, 3);
@arrayOfArray = (
    \@array1,
    \@array2,
    \@array1
);
In the above example,
Code:
$arrayOfArray[0]
and
Code:
$arrayOfArray[2]
are trully the same array (they refer to the same underlying values) while
Code:
$arrayOfArray[0]
and
Code:
$arrayOfArray[1]
are different arrays containing the same values.

In the former case (I mean only true same arrays are considered identical) you could use the numerical value of the reference.
From
Code:
perldoc perlref
:
Code:
Using a reference as a number produces an integer
representing its storage location in memory.
The only useful thing to be done with this is to compare
two references numerically to see whether they refer
to the same location.

if ($ref1 == $ref2) {  # cheap numeric compare of references
     print "refs 1 and 2 refer to the same thing\n";
}

So maybe you just have to force numerical evaluation of the references as you set your hash keys.

Try the following code:
Code:
use strict;

my @array1 = ( 1, 2, 3 );
my @array2 = ( 1, 2, 3 );
my @array3 = ( 1, 2, 4 );

my @arrayOfArrays =
(
 \@array1,
 \@array2,
 \@array1,
 \@array3,
 );

my %hash;
foreach (0 .. $#arrayOfArrays) {
    $hash{0+$arrayOfArrays[$_]} = $_;
}

my @arrayOfUniqArrays =
    map { $arrayOfArrays[$_]; } values %hash;

print "\@arrayOfArrays = (\n    ".
    join("\n    ",
	   map { "(" .
		     join(', ', @{$_}) .
		     "), # $_";}
	   @arrayOfArrays) .
    "\n);\n";

print "\@arrayOfUniqArrays = (\n    ".
    join("\n    ",
	   map { "(" .
		     join(', ', @{$_}) .
		     "), # $_";}
	   @arrayOfUniqArrays) .
    "\n);\n";
 
Ok <wide-eyed> .... impressive.... Mike
________________________________________________________________

&quot;Experience is the comb that Nature gives us, after we are bald.&quot;

Is that a haiku?
I never could get the hang
of writing those things.
 
Wow. I see your logic, but for some reason it doesn't work on my machine. Here's what it prints....

C:\>Testarray.pl
@arrayOfArrays = (
(1, 2, 3), # ARRAY(0x1ab2f30)
(1, 2, 3), # ARRAY(0x1ab2f18)
(1, 2, 3), # ARRAY(0x1ab2f30)
(1, 2, 4), # ARRAY(0x1aaf130)
);
@arrayOfUniqArrays = (
(1, 2, 4), # ARRAY(0x1aaf130)
(1, 2, 3), # ARRAY(0x1ab2f18)
(1, 2, 3), # ARRAY(0x1ab2f30)
);

For some reason it's storing the (1,2,3) arrays in different locations even though they contain the same values.
Cheers,

Sloppyhack
 
It also only works if the refs point to the SAME array and not if there are two arrays with identical elements.
Code:
my @array1 = ( 1, 2, 3 );
my @array2 = ( 1, 2, 3 );
my @array3 = ( 1, 2, 4 );
my @array4 = ( 1, 2, 3 ); # Same as @array1

my @arrayOfArrays =
(
    \@array1,
    \@array2,
    \@array4,
    \@array3,
);
This produces:
Code:
@arrayOfArrays = (
    (1, 2, 3), # ARRAY(0x80fd4ac)
    (1, 2, 3), # ARRAY(0x81007f4)
    (1, 2, 3), # ARRAY(0x8100944)
    (1, 2, 4), # ARRAY(0x81008d8)
);
@arrayOfUniqArrays = (
    (1, 2, 3), # ARRAY(0x8100944)
    (1, 2, 3), # ARRAY(0x81007f4)
    (1, 2, 3), # ARRAY(0x80fd4ac)
    (1, 2, 4), # ARRAY(0x81008d8)
);

jaa
 
What exactly are the specifications for a 'unique' array. Does the order of the array matter? I.e. are (1, 2, 3) and (2, 3, 1) different? Do all arrays have to have the same length?

jaa
 
Quote from my previous post:

Do you want to check that the elements of your array of arrays are references to the true same array or that they are arrays containing the same elements.

The solution I give is for the first solution.
The references (
Code:
# ARRAY(xxxxxx)
) are all different in the arrayOfUniqArrays. It means that you can change any item of one the array without affecting the other arrays. This is not true in the arrayOfArrays. In the example I use, the following code:
Code:
$arrayOfArrays[0][1] = 5;
$arrayOfArrays[2][1] = 6;
$arrayOfArrays[1][1] = 7;
die &quot;What !!! I just put 5 in it !&quot;
    if $arrayOfArrays[0][1] == 6;
will die !
Because arrayOfArrays[0] and arrayOfArrays[2] are the same array, not two different arrays happening to currently have elements with the values.

So, maybe justice41 ask the true question:
What exactly are the specifications for a 'unique' array.
 
Great points folks and some very creative solutions. My definition of duplicate array in this instance are arrays with the same elements in the same order. These arrays do not have the same refs so I don't this will work for me. Fortunately, I was able to create a work around, but it would be great if someone could figure out an algorithm to define arrays that are duplicates. Whether it be that they contain the same elements in different or the same elements in the same order. I love this forum!!!!! Lots of smart people. (excluding myself:) Cheers,

Sloppyhack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top