Hi
As you are new here, you need to first learn how to post questions and what you should do before asking questions here. First you have to try yourself that how you will learn.
Here is the little bit code I write for you, It asks any string(first/last) name and checks with each line of the music.txt file, if matches prints the details as you asked.
use strict;
use warnings;
use Data::Dumper;
open(FH, "<", "./music.txt") or die $!;
while(1)
{
print "Enter Either First or Last name of the artist -1 to qui
+te>";
my $input =<STDIN>;
chomp($input);
if($input eq '-1')
{
exit;
}
if($input =~/^$/)
{
next;
}
&findArtist($input);
}
sub findArtist
{
my $userInput=shift;
my $eachLine;
my ($artistName,$cdTitle,$date,$price);
my $flag=0;
seek(FH, 0, 0);
while($eachLine=<FH>)
{
($artistName,$cdTitle,$date,$price)= split(/:/,$eachL
+ine);
if($artistName =~/$userInput/i){
print "Artist Name:$artistName\n","CD title:$c
+dTitle\n","Date:$date\n","Price:\$$price\n";
$flag=1;
}
}
print "Artist not found\n" unless($flag);
}
close(FH);
Update:
Fixed the bug posted by marto, thanks marto, good exercise for me.
|