Match and return data based on my search criteria

Posted on 2006-03-21
Last Modified: 2010-03-05
I'm kind of new to Perl so I appologize in advance! I'm trying to build a little Movie Database where the movies are stored in a datafile (for now). I've managed to set up adding new movies to the data file, it was pretty simple. I want to be able to search the datafile based on specific fields found in the data file (Like movie title or director, for example)

My code so far:


use CGI ":standard";
use Fcntl qw (:DEFAULT :flock);

print header;
print start_html("Searching Movies");

$movie_file = "../Movies.txt";
open(DAT, $movie_file) || die("Could not open file!");

#this is the search criteria from my html form

#the delimiters in the file are "::" and each new movie is denoted by a new line

foreach $movie (@raw_data)
      ($title, $director, $year, $length, $genre, $rating, $review) =  split (/::/, $movie);
      if ($searchString =~/$title/) {print "Match FOUND. $searchString was found in $title"}

#I'm going to want to display all of the fields for the movie here in a pretty table of some sort

print end_html;

*** end code

So the code above seems pretty simple, I'm just not sure why I'm getting the output that I'm getting. For some reason it is printing $title as a blank character at this point. But if I were just to print $title outside of the IF statement, it will print the correct titles from the datafile. I think I'm misunderstanding how the "foreach $movie" is processing the datafile
Any ideas?


Question by:john946
    LVL 84

    Accepted Solution

    when $title is blank your if stateent would be
     if ($searchString =~//) {print "Match FOUND. $searchString was found in $title"}
    // matches everything, since all strings contain an empty string
    maybe you wanted
    if( $title =~ /$searchString/ ){ print "Match FOUND. $searchString was found in $title" }

    Author Comment

    Yup that was it!
    Thanks for your help!!

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (…
    Checking the Alert Log in AWS RDS Oracle can be a pain through their user interface.  I made a script to download the Alert Log, look for errors, and email me the trace files.  In this article I'll describe what I did and share my script.
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    779 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    16 Experts available now in Live!

    Get 1:1 Help Now