Welcome to Dream.In.Code
Getting Help is Easy!

Join 86,399 Programmers. There are 1,475 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

loop help

 
Reply to this topicStart new topic

loop help

b_real
post 19 Apr, 2008 - 03:31 AM
Post #1


New D.I.C Head

*
Joined: 29 Dec, 2006
Posts: 12



Hi, need to store the results of an SQL search either in a file or a variable for use later on. I had to apply a regex to the search results to filter out on specific criteria and display in a table on the browser. My problem is storing the results of the table into a variable or logfile so I can search on those results later on. Code just now is:

CODE

$sql = ("SELECT title, name, surname,
        year, bids FROM book");

    $sth=$dbh->prepare($sql);
    $sth->execute() ||
    die "Could not execute SQL statement... maybe invalid syntax?";
    
    #show the results of the regex query in a table
    print "<br>Search Results:<br>".
        "<table border=1>";
    while (my @array = $sth->fetchrow_array()){        
        print "<tr>";
        foreach my $elem (@array){
            #search & display the first element in the array for the regex
            $searchTitle=$array[0];
            if ($searchTitle=~/(?=.*pr\w{4}m)(?=(?=\w*w\w*)(?=\w*b\w*)\w+\b)/){
            print "<td>";
            print "$elem\n";
            print "<td>";
            }
        }
    }
    print "</table>";


Up to here is fine, and the program does what i need it to do, which is display the results of the regex in a table. Ive tried printing the results to a logfile as follows:

CODE

$sql = ("SELECT title, name, surname,
        year, bids FROM book");

    $sth=$dbh->prepare($sql);
    $sth->execute() ||
    die "Could not execute SQL statement... maybe invalid syntax?";
    
    #show the results of the regex query in a table
    print "<br>Search Results:<br>".
        "<table border=1>";
    while (my @array = $sth->fetchrow_array()){        
        print "<tr>";
        foreach my $elem (@array){
            #search & display the first element in the array for the regex
            $searchTitle=$array[0];
            if ($searchTitle=~/(?=.*pr\w{4}m)(?=(?=\w*w\w*)(?=\w*b\w*)\w+\b)/){
            print "<td>";
            print "$elem\n";
            print "<td>";

            #PROBLEM IS HERE ONWARDS: This prints out the title element n times where n =number # of columns in the SQL search
            unless (open (LOGFILE, ">>//logs/search1results"))
                {print "Failed to open search1results log!";exit}
            flock (LOGFILE, LOCK_EX);
            print LOGFILE "$searchTitle\n";
            unless (close (LOGFILE))
                {print "Failed to close search1results log!";exit}
            }
        }
    }
    print "</table>";

But the first element of the array gets printed out once each time the program searches a new column. I just need one line for each regex result. Im not sure if the code is right, or if its just in the wrong place (ive tried it in various places, but this seems to work the best), or is there an easier way to store the regex in a variable somehow?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


b_real
post 19 Apr, 2008 - 03:29 PM
Post #2


New D.I.C Head

*
Joined: 29 Dec, 2006
Posts: 12

OK, think I found the answer in the following code:

CODE

push @results_1, join('\t', $searchTitle);


Plus the looping structure is a bit off kilter.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 5/17/08 06:21AM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month