homepage Welcome to WebmasterWorld Guest from 54.145.243.51
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
Remove duplicates, then sort and number, from form POST info.
Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3745770 posted 5:10 pm on Sep 16, 2008 (gmt 0)

Is it possible to change this code...

#!/usr/bin/perl
use CGI ':standard';

my $q=new CGI;
my $code=$q->param('code');

@lines=split (/\n/, $code);

print "Content-type: text/html\n\n";
print "<html>
<head>
<title>Sorted text</title>
<H1>Sorted and numbered text</H1><PRE>";
my $loop=1;
foreach (@lines) {
print $loop."WWW".$_."\n";
$loop++;
};
print "</PRE></body></html>";

to include these little codes...

# Remove duplicates
my %unique;
@unique{map {lc} @lines} = @lines;

# Then sort
print "$unique{$_}<br />" foreach sort {$a cmp $b} keys %unique;

to Remove duplicates and sort, and then number the form POST info?

 

Dabrowski

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3745770 posted 5:43 pm on Sep 16, 2008 (gmt 0)

It's a little difficult to sort without knowing what the data is, and how you want to sort it, could you give an example?

[edited by: Dabrowski at 5:44 pm (utc) on Sep. 16, 2008]

Jesse_Smith

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 3745770 posted 6:12 pm on Sep 16, 2008 (gmt 0)

For example, entering..

yahoo
Commission Junction
google
msn
Microsoft
dmoz
Commission Junction
Azoogle
Amazon
msn
dmoz
ClickBank
Commission Junction
Chitika
dmoz

in to a form and it spits out...

1WWWAmazon
2WWWAzoogle
3WWWChitika
4WWWClickBank
5WWWCommission Junction
6WWWdmoz
7WWWgoogle
8WWWMicrosoft
9WWWmsn
10WWWyahoo

and it can have numbers, letters and characters in the stuff to sort/remove duplicates.

perl_diver

5+ Year Member



 
Msg#: 3745770 posted 6:35 pm on Sep 16, 2008 (gmt 0)

answer already posted on other forum. May as well post it here too:



#!/usr/bin/perl
use CGI ':standard';

my $q=new CGI;
my $code=$q->param('code');
@lines = split (/\n/, $code);
# Remove duplicates
my %unique;
# Remove duplicates
@unique{map {lc} @lines} = @lines;

# Then sort
print "$unique{$_}<br />" foreach sort {$a cmp $b} keys %unique;
# Then sort
print "$unique{$_}<br />" foreach sort {$a cmp $b} keys %unique;

print "Content-type: text/html\n\n";
print "<html>
<head>
<title>Sorted text</title>
<H1>Sorted and numbered text</H1><PRE>";
my $loop=1;
foreach (sort keys %unique) {
print $loop."WWW".$_."\n";
$loop++;
};
print "</PRE></body></html>";



Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved