perl / mysql scripting question

Discussion in 'Programming' started by pc_user, Jul 1, 2006.

  1. #1
    I have 6 values

    1 - The cat and the hat
    2 - We the people of the united states
    3 - The cat ate the hat
    4 - Where are the starts
    5 - the cat wen to the hat
    6- the books are not good

    What is the most efficient way in perl and/or mysql to group them

    search for cat and the

    and return

    The cat and the hat
    The cat ate the hat
    the cat wen to the hat
     
    pc_user, Jul 1, 2006 IP
  2. DXL

    DXL Peon

    Messages:
    380
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I'm not sure if I understand your question right, but you can easily sort arrays alphabetically using the sort function.
    @values = sort { $a cmp $b } @values;

    Maybe you can clarify your question.
     
    DXL, Jul 1, 2006 IP
  3. pc_user

    pc_user Notable Member

    Messages:
    1,891
    Likes Received:
    94
    Best Answers:
    0
    Trophy Points:
    235
    #3
    Imagine a scoring / search algorithm.

    Let's say for example, we are searching for "cat food selections"

    and the data sets are

    - johns cat food
    - html web design
    - mikes cat food
    - dog food is bad
    - cat food is great
    - web designers

    I only want

    - johns cat food
    - mikes cat food
    - cat food is great

    to come back.

    I hope that clears it up a bit.

    Thanks for the help.
     
    pc_user, Jul 1, 2006 IP
  4. DXL

    DXL Peon

    Messages:
    380
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #4
    
    @dataset = ('johns cat food', 'html web design', 'mikes cat food', 'dog food is bad', 'cat food is great', 'web designers');
    $query = 'cat food selection';
    @qwords = split(/ /, $query);
    foreach $_ (@dataset) {
        foreach $qword (@qwords) {
            if(/$qword/) {
                $matches[$i]++;
            }
        }
        $i++;
    }
    for($i=0;$i<($#dataset+1);$i++) {
        print $dataset[$i].':'.int($matches[$i])."\n";
    }
    
    Code (markup):
    Now you only have to determine the highest score and then list all data that have that score.
    This is what it returns:
    
    johns cat food:2
    html web design:0
    mikes cat food:2
    dog food is bad:1
    cat food is great:2
    web designers:0
    
    Code (markup):
     
    DXL, Jul 1, 2006 IP