Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts

    code doesn't generate results im looking for / seeking general suggestions also

    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

  • #2
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by php_user View Post
    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:

    PHP Code:
        $keyword substr($keyword0, (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

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    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:
    PHP Code:
    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

  • Users who have thanked oesxyl for this post:

    php_user (04-30-2011)

  • #4
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    i copyed the script try to debug. As a note, first problem i found is on line 59:

    PHP Code:
        $keyword substr($keyword0, (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?

  • #5
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    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:
    PHP Code:
    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'?

  • #6
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by php_user View Post
    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:

    PHP Code:
    $keyword substr($keyword0, -1); 
    Quote Originally Posted by php_user View Post
    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

  • #7
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by php_user View Post
    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.
    PHP Code:
    output_desired_strings($desired_array$content$max_constraint); 
    PHP Code:
    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 in php can use negative values for start and length and this:
    PHP Code:
    $desired_string substr($content$wallbegin 10$span 10); 
    could be not what you expect to be when $wallbegin is less then 10.

    best regards

  • #8
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    5
    Thanks
    1
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by oesxyl View Post
    no, i'm using linux too, need -1 since the newline is '\n' and you don't need the length, this should be enough:

    PHP Code:
    $keyword substr($keyword0, -1); 
    I see two characters: a '\' and an 'n'. But you are suggesting -1 not -2 in substr. Why is this?

  • #9
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by php_user View Post
    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


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •