We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

Match and return data based on my search criteria

john946
john946 asked
on
Medium Priority
213 Views
Last Modified: 2010-03-05
Hi,
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:

#!/usr/bin/perl

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!");
@raw_data=<DAT>;
close(DAT);

#this is the search criteria from my html form
my($searchString)=(param("searchCriteria"));


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

foreach $movie (@raw_data)
{
      chop($movie);
      ($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?

Thanks
-John


Comment
Watch Question

CERTIFIED EXPERT
Most Valuable Expert 2014
Top Expert 2015
Commented:
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" }

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Author

Commented:
Yup that was it!
Thanks for your help!!
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.