[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


Match and return data based on my search criteria

Posted on 2006-03-21
Medium Priority
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 85

Accepted Solution

ozo earned 400 total points
ID: 16250613
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

ID: 16254166
Yup that was it!
Thanks for your help!!

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
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…
Six Sigma Control Plans
Suggested Courses

834 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