From owner-freebsd-questions@FreeBSD.ORG Wed Apr 26 03:05:26 2006 Return-Path: X-Original-To: questions@freebsd.org Delivered-To: freebsd-questions@FreeBSD.ORG Received: from mx1.FreeBSD.org (mx1.freebsd.org [216.136.204.125]) by hub.freebsd.org (Postfix) with ESMTP id 68D2416A409 for ; Wed, 26 Apr 2006 03:05:26 +0000 (UTC) (envelope-from parv@pair.com) Received: from mta9.adelphia.net (mta9.adelphia.net [68.168.78.199]) by mx1.FreeBSD.org (Postfix) with ESMTP id DCF2F43D48 for ; Wed, 26 Apr 2006 03:05:22 +0000 (GMT) (envelope-from parv@pair.com) Received: from default.chvlva.adelphia.net ([69.160.66.115]) by mta9.adelphia.net (InterMail vM.6.01.05.02 201-2131-123-102-20050715) with ESMTP id <20060426030522.DXDC13882.mta9.adelphia.net@default.chvlva.adelphia.net>; Tue, 25 Apr 2006 23:05:22 -0400 Received: by default.chvlva.adelphia.net (Postfix, from userid 1000) id 08111B770; Tue, 25 Apr 2006 23:05:36 -0400 (EDT) Date: Tue, 25 Apr 2006 23:05:35 -0400 From: Parv To: Nikolas Britton Message-ID: <20060426030535.GA1540@holestein.holy.cow> Mail-Followup-To: Nikolas Britton , FreeBSD Questions References: Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Cc: FreeBSD Questions Subject: Re: Perl: sort string alphabetically, or remove dupe chars? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 26 Apr 2006 03:05:26 -0000 in message , wrote Nikolas Britton thusly... > > On 4/25/06, Nikolas Britton wrote: > > basically what I want to do: ... > > my $wordlist = "letter"; > > ## some whizbang regex that removes dupe chars > > ## from words like "alphabetically" --> "alphbeticy". > > print "$wordlist\n"; ... > This works... but it's clunky: > > my $string = "letter"; > my @chars = split("", $string); > $string = ""; @chars = sort (@chars); > foreach (@chars) { > $string .= $_; > } > $string =~ tr///cs; > print "$string"; You could combine some of the steps ... my $string = 'letter'; $string = join '' , sort split '', $string; $string =~ tr///cs; print $string; ... another but rather clunky version is ... my $string = 'letter'; { my %string; @string{ split '' , $string } = (); $string = join '' , sort keys %string; } print $string; - Parv --