...

View Full Version : code doesn't generate results im looking for / seeking general suggestions also



php_user
04-30-2011, 04:42 PM
The output I'm getting is not matching what I want. I want the strings returned to contain the two keywords and a bit of surrounding text. The array that's dumped looks ok, but not the strings that are generated based on the array, as you can see in the sample session provided in the codepad post. I've spent hours trying to resolve this. Any help is appreciated.

http://codepad.org/MUjrxavI

oesxyl
04-30-2011, 05:19 PM
The output I'm getting is not matching what I want. I want the strings returned to contain the two keywords and a bit of surrounding text. The array that's dumped looks ok, but not the strings that are generated based on the array, as you can see in the sample session provided in the codepad post. I've spent hours trying to resolve this. Any help is appreciated.

http://codepad.org/MUjrxavI
i copyed the script try to debug. As a note, first problem i found is on line 59:



$keyword = substr($keyword, 0, (strlen($keyword) -2));

this is platform dependent, on windows where '\r\n' is the newline is probably correct but in rest $keyword will be 1 char shorter.
i will come with some feedback.

best regards

oesxyl
04-30-2011, 06:12 PM
i don't understand what find_desired_strings you want to do but for proper value of input it work:
for example i used:
first keyword: 'note'
second keyword: 'convert'
max: 60
min: 10

i change the code for debug this way:


function find_desired_strings($array_keyword_1, $array_keyword_2, $max_constraint, $min_constraint){
$desired_array = array();
foreach ($array_keyword_1 as $keyword_1_position){
foreach ($array_keyword_2 as $keyword_2_position){
$dif = abs ($keyword_1_position - $keyword_2_position);
if ((abs ($keyword_1_position - $keyword_2_position) <= $max_constraint) &&
(abs ($keyword_1_position - $keyword_2_position) >= $min_constraint)){
array_push($desired_array, $keyword_1_position, $keyword_2_position);
print 'true: ' . $keyword_1_position . ", ". $keyword_2_position . ", " . $dif . "\n";
} else{
print 'false: ' . $keyword_1_position . ", ". $keyword_2_position . ", " . $dif . "\n";
}
}
}
return $desired_array;
}


best regards

php_user
04-30-2011, 07:34 PM
i copyed the script try to debug. As a note, first problem i found is on line 59:



$keyword = substr($keyword, 0, (strlen($keyword) -2));

this is platform dependent, on windows where '\r\n' is the newline is probably correct but in rest $keyword will be 1 char shorter.
i will come with some feedback.

best regards

Thanks for replying.
I used the '-2' above to remove the '\n' added by STDIN so I can have the user's correct keyword input.
I am using linux, so in my case -2 would be the correct number to use for my purposes, or no?

php_user
04-30-2011, 07:39 PM
i don't understand what find_desired_strings you want to do but for proper value of input it work:
for example i used:
first keyword: 'note'
second keyword: 'convert'
max: 60
min: 10

i change the code for debug this way:


function find_desired_strings($array_keyword_1, $array_keyword_2, $max_constraint, $min_constraint){
$desired_array = array();
foreach ($array_keyword_1 as $keyword_1_position){
foreach ($array_keyword_2 as $keyword_2_position){
$dif = abs ($keyword_1_position - $keyword_2_position);
if ((abs ($keyword_1_position - $keyword_2_position) <= $max_constraint) &&
(abs ($keyword_1_position - $keyword_2_position) >= $min_constraint)){
array_push($desired_array, $keyword_1_position, $keyword_2_position);
print 'true: ' . $keyword_1_position . ", ". $keyword_2_position . ", " . $dif . "\n";
} else{
print 'false: ' . $keyword_1_position . ", ". $keyword_2_position . ", " . $dif . "\n";
}
}
}
return $desired_array;
}


best regards

You're saying it works with proper input? That is good. I had suspected the function, 'output_desired_strings', might be the culprit. the point of function, 'find_desired_strings', is two gather the positions of the keywords so that 'output_desired_strings' can print them. so it creates an array like [keyword1_position1, keyword2_position1, keyword1_position2, keyword2_position2, etc.]

do you see any issues with 'output_desired_strings'?

oesxyl
04-30-2011, 09:05 PM
Thanks for replying.
I used the '-2' above to remove the '\n' added by STDIN so I can have the user's correct keyword input.
I am using linux, so in my case -2 would be the correct number to use for my purposes, or no?
no, i'm using linux too, :) need -1 since the newline is '\n' and you don't need the length, this should be enough:



$keyword = substr($keyword, 0, -1);



You're saying it works with proper input? That is good. I had suspected the function, 'output_desired_strings', might be the culprit. the point of function, 'find_desired_strings', is two gather the positions of the keywords so that 'output_desired_strings' can print them. so it creates an array like [keyword1_position1, keyword2_position1, keyword1_position2, keyword2_position2, etc.]

do you see any issues with 'output_desired_strings'?
i didn't look yet to 'output_desired_strings', just stop to 'find_desired_strings' because i didn't know if is ok or not. In fact i still don't understand the reason you use $max_constraint, $min_constraint to filter the output.
i will look over 'output_desired_strings', :)

best regards

oesxyl
05-01-2011, 01:06 AM
do you see any issues with 'output_desired_strings'?
i can't check the logic since i don't understand what you want to do. One problem with 'output_desired_strings' is that you call it with three arguments, last one is $max_constraint but in function definition you have only two, and you use the default value 10 instead of last one.


output_desired_strings($desired_array, $content, $max_constraint);



function output_desired_strings($desired_array, $content){

i guess after your coding style that you use c often then php and i don't know if you are aware of some difference between the php functions you used and their c equivalent. For example substr (http://www.php.net/manual/en/function.substr.php) in php can use negative values for start and length and this:


$desired_string = substr($content, $wallbegin - 10, $span + 10);

could be not what you expect to be when $wallbegin is less then 10.

best regards

php_user
05-01-2011, 02:25 AM
no, i'm using linux too, :) need -1 since the newline is '\n' and you don't need the length, this should be enough:



$keyword = substr($keyword, 0, -1);



I see two characters: a '\' and an 'n'. But you are suggesting -1 not -2 in substr. Why is this?

oesxyl
05-01-2011, 02:34 AM
I see two characters: a '\' and an 'n'. But you are suggesting -1 not -2 in substr. Why is this?
\n is the representation of a single character, 0x0a, new line.
\r means 0x0d and is also a single character.

best regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum