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 Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find Dups from Array Itself

Status
Not open for further replies.

perln00b

Programmer
Oct 4, 2005
21
US
I really new in Perl. I tried to create a simple script to
find the duplicate items in array. Here is my source code:
###########################################################
@arry = ("1", "2", "2", "2", "3", "3");
@dups = ();
@copiedArry = @arry;

$size = @arry;
$arryLoc = $size - 1;

for ($i=0; $i<=$arryLoc; $i++)
{
for ($j=0; $j<=$arryLoc; $j++)
{
while ($size != 0)
{
if($copiedArry[$i] eq $arry[$j]) # <==
{
push(@dups, $arry[$j]);
delete $arry[$j];
$size --;
}
else
{
$size --;
}
}
}
}

print "Here are the dups: \n";
foreach $data (@dups)
{
print $data ."\n";
}

##########################################################
see line with #<==, I got "Use of uninitialized value in string eq at
C:\Perl Projects\testing.pl line 17."
Is the way I created correct? Why I got "uninitialized value..." like that?

Thanks,
Lucas
 
I didn't try and debug your code since this is so much easier using a hash:

Code:
@arry = ("1", "2", "2", "2", "3", "3");
map {$dups{$_}++} @arry;

print "Here are the dups:\n";

for (keys %dups) {
   print "$dups{$_}\n" if $dups{$_} > 1;
}

I know for a newbie perl coder that probably looks like Greek. You have this:

delete $arry[$j];

delete() is for use with hashes, not arrays, so it isn't doing what you think/hope.
 
Try this:
Code:
#!/usr/bin/perl -w
use strict;
my @arry  = ("1", "2", "2", "2", "3", "3");
my %uniq  = ();
my @clean = grep { ! $uniq{$_}++;  } @arry;
print "Cleaned array is [@clean]\n";
my @dups  = map { $uniq{$_} > 1 ? ($_) : (); } sort keys %uniq;
print "Dupes are [@dups]\n";


Trojan.
 
Thank you, guys.
Both scripts are working great.


Lucas
 
Figures, when I win the race, the prize is for being second... thats the story of my life! [sad] [thumbsup]
 
Delete actually works on arrays, hashes, array slices and hash slices since about perl 5.004 or 5.005 iirc...

Kordaff
 
delete() sort of works on an array, but it probably is not doing what a person thinks:

Code:
my @array = ('a'  .. 'z');
print "@array\n";
print scalar @array; [b]# prints 26[/b]
delete $array[0];
print scalar @array; [b]# still prints 26[/b]
print "@array";

basically turns the deleted scalar into an empty string.
for a hash it works like expected:

Code:
%hash = ();
map {$hash{$_}++} ('a'..'z');
print scalar keys %hash; [b]#prints 26[/b]
print "$_\n" for sort keys %hash;
delete $hash{a};
print scalar keys %hash; [b]#prints 25[/b]
print "$_\n" for sort keys %hash;

the hash key/value are deleted
 
Ahh, only shrinks if it's last item in array. That's a lil broken heh. Best use splice then.

Kordaff
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top