Click here to get back home

checking for duplicates in a string

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
checking for duplicates in a string John 02-23-2008
Posted by John on February 23, 2008, 6:18 am
Please log in for more thread options
Hi

Consider a string $packed consisting of "AAXYGHDRJKYYXYZZGH"
This represent items of two chars eg AA XY GH etc.
I need to remove duplicates.
I can do it by 'unpacking' and repacking using two loops such as

for (my $j=0; $j<$total; $j=$j+2) {
my $part1=substr($packed,$j,2);
etc etc.

But is there a better, dare I say, elegant, way?

Regards
John



Posted by Gunnar Hjalmarsson on February 23, 2008, 6:36 am
Please log in for more thread options
John wrote:
> Consider a string $packed consisting of "AAXYGHDRJKYYXYZZGH"
> This represent items of two chars eg AA XY GH etc.
> I need to remove duplicates.
> I can do it by 'unpacking' and repacking using two loops such as
>
> for (my $j=0; $j<$total; $j=$j+2) {
> my $part1=substr($packed,$j,2);
> etc etc.
>
> But is there a better, dare I say, elegant, way?

my $packed = 'AAXYGHDRJKYYXYZZGH';
my %seen;
print join(' ', map $seen++ ? () : $_, $packed =~ /../g), "\n";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Gunnar Hjalmarsson on February 23, 2008, 7:39 am
Please log in for more thread options
Gunnar Hjalmarsson wrote:
>
> my $packed = 'AAXYGHDRJKYYXYZZGH';
> my %seen;
> print join(' ', map $seen++ ? () : $_, $packed =~ /../g), "\n";

The grep() function is more to the point:

print join(' ', grep !$seen++, $packed =~ /../g), "\n";

--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl

Posted by Ben Morrow on February 23, 2008, 7:07 pm
Please log in for more thread options

> John wrote:
> > Consider a string $packed consisting of "AAXYGHDRJKYYXYZZGH"
> > This represent items of two chars eg AA XY GH etc.
> > I need to remove duplicates.
> > I can do it by 'unpacking' and repacking using two loops such as
> >
> > for (my $j=0; $j<$total; $j=$j+2) {
> > my $part1=substr($packed,$j,2);

Hmm... even if you *did* want to unpack, this is a bad way to do it.
Gunnar's /../g is much better, and unpack '(A2)*' better again.

> > etc etc.
> >
> > But is there a better, dare I say, elegant, way?
>
> my $packed = 'AAXYGHDRJKYYXYZZGH';
> my %seen;
> print join(' ', map $seen++ ? () : $_, $packed =~ /../g), "\n";

That still unpacks and repacks.

s/(..)/$seen++ ? '' : $1/ge;

Ben


Posted by John on February 24, 2008, 6:31 am
Please log in for more thread options

>
>> John wrote:
>> > Consider a string $packed consisting of "AAXYGHDRJKYYXYZZGH"
>> > This represent items of two chars eg AA XY GH etc.
>> > I need to remove duplicates.
>> > I can do it by 'unpacking' and repacking using two loops such as
>> >
>> > for (my $j=0; $j<$total; $j=$j+2) {
>> > my $part1=substr($packed,$j,2);
>
> Hmm... even if you *did* want to unpack, this is a bad way to do it.
> Gunnar's /../g is much better, and unpack '(A2)*' better again.
>
>> > etc etc.
>> >
>> > But is there a better, dare I say, elegant, way?
>>
>> my $packed = 'AAXYGHDRJKYYXYZZGH';
>> my %seen;
>> print join(' ', map $seen++ ? () : $_, $packed =~ /../g), "\n";
>
> That still unpacks and repacks.
>
> s/(..)/$seen++ ? '' : $1/ge;
>
> Ben
>

Many thanks. It is taken me most of the morning to work out why it works,
but it does.
Thanks again
John




Similar ThreadsPosted
duplicates May 29, 2007, 1:33 pm
array - removing duplicates November 14, 2004, 10:43 pm
Removing duplicates with RegExp December 7, 2005, 9:48 pm
Find duplicates in a dat file March 29, 2006, 5:49 am
Filtering duplicates out of an array October 11, 2006, 6:04 am
Sort and remove duplicates September 28, 2007, 9:10 am
deleting duplicates in array using references July 30, 2007, 2:37 pm
Working with Duplicates in Perl to generate Unique ID June 17, 2005, 8:48 am
identifying the duplicate when removing duplicates from any array June 10, 2006, 2:14 pm
Merge multiple rows and remove duplicates --based on the first value January 26, 2006, 5:47 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap