Skip site navigation (1)Skip section navigation (2)
Date:      Thu, 27 Apr 2006 15:10:35 -0500
From:      "Nikolas Britton" <nikolas.britton@gmail.com>
To:        "Andrew Reitz" <ajr9@po.cwru.edu>
Cc:        FreeBSD Questions <questions@freebsd.org>
Subject:   Re: Perl: sort string alphabetically, or remove dupe chars?
Message-ID:  <ef10de9a0604271310j502e2720wc04414bfd4aded28@mail.gmail.com>
In-Reply-To: <C9CD63DC-3BEA-41DD-9005-7D02926061F4@po.cwru.edu>
References:  <ef10de9a0604251540p6461bfedgf788d500a81e7190@mail.gmail.com> <ef10de9a0604251754r3292719dqc29d96095a9f0752@mail.gmail.com> <20060426030535.GA1540@holestein.holy.cow> <ef10de9a0604260034y7c27ca88l417f8fc0f495859a@mail.gmail.com> <C9CD63DC-3BEA-41DD-9005-7D02926061F4@po.cwru.edu>

next in thread | previous in thread | raw e-mail | index | archive | help
On 4/27/06, Andrew Reitz <ajr9@po.cwru.edu> wrote:
>
> On Apr 26, 2006, at 12:34 AM, Nikolas Britton wrote:
>
> > On 4/25/06, Parv <parv@pair.com> wrote:
> [snip]
> > 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 =3D `sed /^$sedstring1\\\$/\\!d < enable2k_wordlist`;
> > foreach (@wordlist1) {
> >   $string =3D $_;
> >   $string =3D~ s/[$solvedletters1]//g;
> >   my @chars =3D split("", $string);
> >   $string =3D ""; @chars =3D sort (@chars);
> >     foreach (@chars) {
> >       $string .=3D $_;
> >     }
> >   $string =3D~ tr///cs;
> >   print "$string";
> > }
>
> Hi Nikolas,
>
> Most likely, your input has '\n' characters at the end of every line,
> and you aren't doing anything in perl to strip those away. Try adding
> a 'chomp($string);' line before you print.
>

Hey thanks, I think I did try that already... anyways... it doesn't
matter now because I reworked the code block, this is what I have so
far:

open(DATA, "< $wordlistfile")
    or die "Couldn't open $wordlistfile for reading: $!\n";

$regex =3D qr{^$sedstring1$};
my %freq;    #keys =3D characters, values =3D frequency count
while (<DATA>) {
   chomp;
   if (/$regex/) {
      push @guesswords, "\n$_";
      $_ =3D~ s/[$solvedletters1]//g;
      my @chars =3D split("", $_);
      $_ =3D ""; @chars =3D sort (@chars);
      foreach (@chars) {
         $foobar3 .=3D $_;
      }
      $_ =3D "$foobar3"; $foobar3 =3D "";
      $_ =3D~ tr///cs;
      #print "$_\n"; # For debugging
      $freq{$_}++ for  split(//, lc $_);
   }
}

$_ =3D scalar @guesswords;
print " Guesswords in list: $_\n";
if (@guesswords <=3D "20") {
   print "@guesswords\n\n";
}

# Print probabilities list.
foreach $_ (sort {$freq{$a} <=3D> $freq{$b}} (keys(%freq))) {
   print " $_\t=3D>\t$freq{$_}\n";
}


--
BSD Podcasts @ http://bsdtalk.blogspot.com/



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