Home: Perl Programming Help: Beginner:
duplicates in an array?



$0.05$
Novice

Jun 1, 2002, 10:29 AM


Views: 7349
duplicates in an array?

how would i go about deleting duplicate strings in an array?


mhx
Enthusiast / Moderator

Jun 1, 2002, 3:18 PM


Views: 7349
Re: [$0.05$] duplicates in an array?

Here's a solution (as far as I can recall it's the same as in the Perl Cookbook) that uses a hash to keep track of the strings already seen in the array:


Code
@no_duplicates = grep !$seen{$_}++, @strings;


Hope this helps.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



$0.05$
Novice

Jun 1, 2002, 5:18 PM


Views: 7344
Re: [mhx] duplicates in an array?

ok, what if there were triplicates, and all i wanted was singles??


$0.05$
Novice

Jun 1, 2002, 5:27 PM


Views: 7344
Re: [$0.05$] duplicates in an array?

and it isnt working - perhaps theres a flaw in my code




Code
    

#!/usr/bin/perl
use CGI qw(:standard);
use CGI::Carp 'fatalsToBrowser';

print "Content-type:text/html\n\n";


$action = param('action');
$input = param('first');

if ($action eq "1"){
sort_array();
}
else {
main();
}

sub main{
print qq(
Insert all email addresses in here<br>
<form action ="sort.pl" method="post">
<input type="hidden" name="action" value="1">
<textarea name="first" rows="10" cols="40"></textarea><br>
<input type="submit" value="Let's get rid of those dup's!">
</form>
);

}

sub sort_array{
print "Sorting..<br>";
(@input_done) = split(/\n/, $input);
@sorted = sort { $a <=> $b } @input_done;

@no_duplicates1 = grep !$seen{$_}++, @sorted;

print qq(<textarea rows="10" cols="40">);
foreach $thing (@no_duplicates1){
print "$thing\n";
}
print qq(</textarea>);

}



i will input this


Code


and get this
ryan@lonegamers.com--
blah@blan.com--
blah@blah.com--



(This post was edited by $0.05$ on Jun 1, 2002, 5:28 PM)


mhx
Enthusiast / Moderator

Jun 2, 2002, 2:37 AM


Views: 7339
Re: [$0.05$] duplicates in an array?

As far as I can see it's working perfectly (and it also does for triplicates, by the way). I don't think you want my code to treat lowercase N and H as if they were the same character, even if they look kinda similar... Wink


Code
ryan@lonegamers.com 
blah@blan.com
blah@blah.com


-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



(This post was edited by mhx on Jun 2, 2002, 2:39 AM)


$0.05$
Novice

Jun 2, 2002, 2:10 PM


Views: 7333
Re: [mhx] duplicates in an array?

it still does not seem to be working correctly. try it at http://test-me.netfirms.com/cgi-bin/sort.pl

perhaps i could contact you on icq/msn/aim?

icq:63088794
msn:snowboardinca@hotmail.com
aim:ultimategamer4l


mhx
Enthusiast / Moderator

Jun 3, 2002, 12:01 AM


Views: 7329
Re: [$0.05$] duplicates in an array?


In Reply To
try it at http://test-me.netfirms.com/cgi-bin/sort.pl


This doesn't give me any output at all... Unsure

I'm sure there's a problem with the input string itself if you don't terminate the last line with a newline sequence. If you do this using a Windows-based browser, your input will be:


Code
foo\r\nbar\r\nfoo\r\nbar


Since you split on '\n', your array becomes:


Code
@input_done = ( "foo\r", "bar\r", "foo\r", "bar" );


My code only removes _exact_ duplicates, and "bar\r" and "bar" are indeed different, although it's not visible. Thus, you'll get the following output:


Code
foo 
bar
bar


To make a long story short, try to change your split line to:


Code
@input_done = split /\r?\n/, $input;


which will allow an optional '\r' in front of the '\n' and try again.

Hope this helps.

-- mhx

At last with an effort he spoke, and wondered to hear his own words, as if some other will was using his small voice. "I will take the Ring," he said, "though I do not know the way."

-- Frodo



$0.05$
Novice

Jun 3, 2002, 12:46 PM


Views: 7325
Re: [mhx] duplicates in an array?

thanks, works perfect now :)