2
\$\begingroup\$

Though i have never worked on perl, not even once. I have ported the perl code into PHP and it seems like giving a correct result. But as I am going to use it on production server.

I want this code to get it reviewed, please let me know if there is any problem in the porting from perl to PHP.

#!/usr/bin/perl
use strict;
my $WORD_FILE = '/usr/share/dict/words'; #Change as needed
my %words; # Hash of words in dictionary
# Open dictionary, load words into hash
open(WORDS, $WORD_FILE) or die "Failed to open dictionary: $!\n";
while (<WORDS>) {
 chomp;
 $words{lc($_)} = 1;
}
close(WORDS);
# Read one line at a time from stdin, break into words
while (<>) {
 chomp;
 my @words;
 find_words(lc($_));
}
sub find_words {
 # Print every way $string can be parsed into whole words
 my $string = shift;
 my @words = @_;
 my $length = length $string;
 foreach my $i ( 1 .. $length ) {
 my $word = substr $string, 0, $i;
 my $remainder = substr $string, $i, $length - $i;
 # Some dictionaries contain each letter as a word
 next if ($i == 1 && ($word ne "a" && $word ne "i"));
 if (defined($words{$word})) {
 push @words, $word;
 if ($remainder eq "") {
 print join(' ', @words), "\n";
 return;
 } else {
 find_words($remainder, @words);
 }
 pop @words;
 }
 }
 return;
}

The PHP code which I want a review on:

<?php
$WORD_FILE = file_get_contents ("word.txt") ;
$temp_arr = explode("\n", $WORD_FILE);
foreach ($temp_arr as $str)
{
 $words [$str] = 1 ;
}
$processed = Array () ;
find_words ($argv[1]) ; 
print_r ($processed) ;
function find_words ($str)
{
 global $words ;
 global $processed ;
 $string = $str ;
 $length = strlen ($str) ;
 for ($i = 1 ; $i <= $length + 1; $i++)
 {
 $word = substr ($string, 0, $i) ;
 $remainder = substr ($string, $i, $length - $i) ;
 if ($i == 1 && ($word != "a" && $word != "i")) ;
 if (array_key_exists ($word, $words))
 {
 array_push($processed, $word) ;
 if ($remainder == "")
 {
 return ;
 }
 else
 {
 find_words ($remainder, $words) ;
 }
 echo "popping the word " . array_pop ($words) . "\n";
 }
 }
 return ;
}
?>
SuperBiasedMan
13.5k5 gold badges37 silver badges62 bronze badges
asked Feb 24, 2016 at 7:04
\$\endgroup\$
4
  • \$\begingroup\$ Welcome to Code Review! You're mostly interested in just having the PHP reviewed, correct? It sounded like that, but I'm not entirely sure. \$\endgroup\$ Commented Feb 24, 2016 at 9:19
  • \$\begingroup\$ Yes, I want to know whether the PHP code will work correctly and give correct results or not. \$\endgroup\$ Commented Feb 24, 2016 at 13:27
  • \$\begingroup\$ Ouch! However, despite its glam looks it doesn't work. I'm working on a post \$\endgroup\$ Commented Feb 29, 2016 at 17:34
  • \$\begingroup\$ Your original Perl still needs a lot of work, and I am struggling to believe that you didn't know that. If you're having trouble getting it going, then you should post it on Stack Overflow instead, together with your sample input, and required and actual output \$\endgroup\$ Commented Feb 29, 2016 at 18:19

2 Answers 2

1
\$\begingroup\$

This should be find_words() php equivalent, (note perl %words is here $Gwords as perl makes distinction between hashes/dictionaries and arrays)

function find_words() {
 global $Gwords;
 # Print every way $string can be parsed into whole words
 $words = func_get_args();
 $string = array_shift($words);
 $length = strlen($string);
 foreach (range(1, $length) as $i) {
 $word = substr($string, 0, $i);
 $remainder = substr($string, $i, $length - $i);
 # Some dictionaries contain each letter as a word
 if ($i == 1 && ($word !== "a" && $word !== "i")) continue;
 if (isset($Gwords[$word])) {
 $words[]= $word;
 if ($remainder === "") {
 echo join(' ', $words), "\n";
 return;
 }
 else {
 # find_words($remainder, @words);
 call_user_func_array(
 "find_words",
 array_merge((array)$remainder, $words)
 );
 }
 array_pop($words);
 }
 }
 return;
}
answered Feb 24, 2016 at 12:06
\$\endgroup\$
1
\$\begingroup\$

If I use this file in place of /usr/share/dict/words

words.txt

a
ab
abc
i
ij
ijk
x
xy
xyz

and this file as input

input.txt

Adam
Abacab
Nnapric
Alejandro
Dillijence
Alicia
Ambrose
Caleb
Abbey
xyzygy

Then your Perl code produces no output at all. As far as I can tell, these "words" should be found

a
ab
i
ij
xy
xyz

There is absolutely no point in asking for a code review of a translation of a non-functional program

answered Feb 29, 2016 at 18:15
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.