Perl: sort string alphabetically, or remove dupe chars?
Nikolas Britton
nikolas.britton at gmail.com
Wed Apr 26 07:41:27 UTC 2006
On 4/25/06, Parv <parv at pair.com> wrote:
> in message <ef10de9a0604251754r3292719dqc29d96095a9f0752 at mail.gmail.com>,
> wrote Nikolas Britton thusly...
> >
> > 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;
>
Thanks parv... I meant the algorithm was clunky, not the code... I'm
so new at this that I can't read your code, and I have a hard enough
time reading mine own :-).
I've got another simple problem now. How do I get this code to stop
printing everything on a newline, I'm not using \n in my print
statement so why does it do that and how do I get it to stop?
@wordlist1 = `sed /^$sedstring1\\\$/\\!d < enable2k_wordlist`;
foreach (@wordlist1) {
$string = $_;
$string =~ s/[$solvedletters1]//g;
my @chars = split("", $string);
$string = ""; @chars = sort (@chars);
foreach (@chars) {
$string .= $_;
}
$string =~ tr///cs;
print "$string";
}
Then I have to make a probabilty engine to count the frequency of the
letters I get from the above code block. looking for something simple
like this: http://www.amstat.org/publications/jse/secure/v7n2/count-char.cfm
--
BSD Podcasts @ http://bsdtalk.blogspot.com/
More information about the freebsd-questions
mailing list