in reply to Re: Perl Golf idea
in thread Perl Golf idea

haha yes the WORST i could do is convert it to perl! im ashamed of posting it yet.. im trying to make it better. but here is the c version:
string myword; string wlist = "dict.txt";string line; int strLen = myword.length( +);int c = 0; ifstream cfile(wlist); if (cfile.is_open()) { while (cfile.good()) { getline (cfile,line); if (strLen == line.length()) { bool match[strLen];string used[strLen];int matchcount = + 0; for (int i = 0; i < strLen; i++) { string ch = myword.substr(i, 1); for (int y = 0; y < strLen; ++y) { string kh = line.substr(y, 1); if (ch == kh) { if (used[y] == ch) match[matchcount] = 0; else { used[y] = ch; match[matchcount] = 1; matchcount++; break; } } } } c = 0; for (int t = 0; t < strLen; t++){if (match[t] == true) + ++c; } if (c == strLen) { return line; } delete [] match; } } cfile.close(); }
I have a feeling that people think i'm getting them to do my "homework" ? seriously?

Replies are listed 'Best First'.
Re^3: Perl Golf idea
by GrandFather (Saint) on Apr 27, 2012 at 23:38 UTC

    Rewritten in Cerl (C++ transliterated into something that a Perl interpreter in a good mood may care to execute):

    my $myword; my $wlist = "dict.txt"; my $strLen = length $myword; open my $cfile, '<', $wlist or die "Can't open $wlist: $!\n"; while (defined (my $line = <$cfile>)) { next if $strLen != length $line; my @match; my @used; my $matchcount = 0; for (my $i = 0; $i < $strLen; $i++) { my $ch = substr $myword, $i, 1; for (my $y = 0; $y < $strLen; ++$y) { next if $ch ne substr $line, $y, 1; if ($used[$y] eq $ch) { $match[$matchcount] = 0; } else { $used[$y] = $ch; $match[$matchcount] = 1; $matchcount++; last; } } } my $c = 0; for (my $t = 0; $t < $strLen; $t++) { ++$c if ($match[$t]); } return $line if ($c == $strLen); } close $cfile;

    This is of course completely untested and not what I would consider to be Perlish, although it is strict compliant. Note that the minor structure changes I made could actually be pushed back to the original C++ code to clean up that code somewhat.

    True laziness is hard work