Skip site navigation (1)Skip section navigation (2)
Date:      Tue, 25 Apr 2006 23:05:35 -0400
From:      Parv <parv@pair.com>
To:        Nikolas Britton <nikolas.britton@gmail.com>
Cc:        FreeBSD Questions <questions@freebsd.org>
Subject:   Re: Perl: sort string alphabetically, or remove dupe chars?
Message-ID:  <20060426030535.GA1540@holestein.holy.cow>
In-Reply-To: <ef10de9a0604251754r3292719dqc29d96095a9f0752@mail.gmail.com>
References:  <ef10de9a0604251540p6461bfedgf788d500a81e7190@mail.gmail.com> <ef10de9a0604251754r3292719dqc29d96095a9f0752@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
in message <ef10de9a0604251754r3292719dqc29d96095a9f0752@mail.gmail.com>,
wrote Nikolas Britton thusly...
>
> On 4/25/06, Nikolas Britton <nikolas.britton@gmail.com> 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

-- 




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20060426030535.GA1540>