CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

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

 



$0.05$
Novice

Jun 1, 2002, 10:29 AM

Post #1 of 8 (3743 views)
duplicates in an array? Can't Post

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


mhx
Enthusiast / Moderator

Jun 1, 2002, 3:18 PM

Post #2 of 8 (3743 views)
Re: [$0.05$] duplicates in an array? [In reply to] Can't Post

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

Post #3 of 8 (3738 views)
Re: [mhx] duplicates in an array? [In reply to] Can't Post

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


$0.05$
Novice

Jun 1, 2002, 5:27 PM

Post #4 of 8 (3738 views)
Re: [$0.05$] duplicates in an array? [In reply to] Can't Post

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

Post #5 of 8 (3733 views)
Re: [$0.05$] duplicates in an array? [In reply to] Can't Post

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

Post #6 of 8 (3727 views)
Re: [mhx] duplicates in an array? [In reply to] Can't Post

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

Post #7 of 8 (3723 views)
Re: [$0.05$] duplicates in an array? [In reply to] Can't Post


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

Post #8 of 8 (3719 views)
Re: [mhx] duplicates in an array? [In reply to] Can't Post

thanks, works perfect now :)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives