in reply to extraction of data

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.


All is well

Replies are listed 'Best First'.
Re^2: extraction of data
by marto (Cardinal) on May 14, 2014 at 13:42 UTC

    Consider the following points. You search the entire line, rather than the artist name, example data:

    Seamus McGuire:The Wishing Tree:09-14-2000:14.95 Foo:Bar:01-01-2000:15.00 Bar:Baz:01-01-2000:150.00

    Searching for Bar:

    Entery Either First or Last name of the artist:-1 to quite>Bar Artist Name:Foo CD title:Bar Date:01-01-2000 Price:$15.00 Artist Name:Bar CD title:Baz Date:01-01-2000 Price:$15.00 Entery Either First or Last name of the artist:-1 to quite>

    To resolve this split the line into individual fields and match on the artist name only. You could also remove the $flag variable and just add an else to your if. Note that currently your searches are case sensitive. I know OP was not very specific when posting, but many people tend not to consider case to when using search interfaces. A slightly amended prompt:

    print "Enter either First or Last name of the artist:-1 to quit> ";

    Now here's the interesting part, restart the program and search for "Bar" twice:

    Entery Either First or Last name of the artist:-1 to quite>Bar Artist Name:Foo CD title:Bar Date:01-01-2000 Price:$15.00 Artist Name:Bar CD title:Baz Date:01-01-2000 Price:$150.00 Entery Either First or Last name of the artist:-1 to quite>Bar Artist not found Entery Either First or Last name of the artist:-1 to quite>

    I'll leave this as an excercise for you :)

    Update: Strike out nonsense, the pitfalls of my poor multi tasking.

      If i remove the $flag and add else it prints "Artist not found" for each line that does not match the user input.


      All is well