Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

extraction of data

by crazyghost (Initiate)
on May 14, 2014 at 09:32 UTC ( [id://1085995]=perlquestion: print w/replies, xml ) Need Help??

crazyghost has asked for the wisdom of the Perl Monks concerning the following question:

HELLO MONKS This is the text data in a file music.txt
Seamus McGuire:The Wishing Tree:09-14-2000:14.95 Pat Kilbride:Loose Cannon:07-02-2000:15.95 Kevin Crawford:Seasons of Mists:06-23-2000:16.95 Prince:Purple Rain:01-01-1995:3.95 Meat Loaf:Bat out of Jell:03-03-1980:11.95
and I have to create a perl script so that 1) asks for artist's name(either first or last) from user, and
(2) displays artist's full name, CD title, date, and price in formatted output.
Proper heading, w/ commands of your choice, is expected
I have been trying so much, I couldn't get it, any help ?

Replies are listed 'Best First'.
Re: extraction of data
by Corion (Patriarch) on May 14, 2014 at 09:33 UTC

    So, please show us the code that you've already written and also explain what it should do and how your current code fails to do that.

    This helps us help you much better, because we then see at what knowledge level you are and where we can best improve your learning of Perl.

Re: extraction of data
by AppleFritter (Vicar) on May 14, 2014 at 10:08 UTC

    In addition to showing us the code you already got, as Corion suggested, could you also add a bit of formatting to your post so we'll know how your data is structured? I got a hunch there's linebreaks in there somewhere, and it'd be helpful to know where they are.

    Here's some nodes with helpful information:

    You'll make it much easier for us to help you that way.

Re: extraction of data
by Utilitarian (Vicar) on May 14, 2014 at 10:29 UTC
    If you were to attempt to do this manually, what would be the steps?
    • Read the data source into a data structure using the name as an index (see associative arrays in perldoc perldata)
    • Ask for partial name chomp(my $name=<STDIN>);
    • Perform a case insensitive partial match of the name against the index (see perlre)
    • Iterate through the indices of the associative array printing out any that match the test above see perlsyn (foreach) and also the keys operator of associative arrays
    Now all you have to do is try and write that in Perl, it'll break the first time and when you get rid of the error messages it won't do what you intended, but that's true of most of our projects,(and anyone who denies it is probably lying) to quote Greg Lemond "It doesn't get easier, you just get faster".

    So put up the code you wrote and we'll try and hack it into shape, providing you with a complete solution is not helping you in the longer term.

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: extraction of data
by vinoth.ree (Monsignor) on May 14, 2014 at 13:11 UTC

    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

      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
Re: extraction of data
by Lennotoecom (Pilgrim) on May 14, 2014 at 11:40 UTC
    consider this
    chomp, push @a, [split /:/, $_] for <DATA>; print "name? " and chomp($i = <STDIN>); @$_[0] =~/$i/i and print "\n@$_[0]\n@$_[1] (@$_[2]) @$_[3]\$\n" for @a +; __DATA__ Seamus McGuire:The Wishing Tree:09-14-2000:14.95 Pat Kilbride:Loose Cannon:07-02-2000:15.95 Kevin Crawford:Seasons of Mists:06-23-2000:16.95 Prince:Purple Rain:01-01-1995:3.95 Meat Loaf:Bat out of Jell:03-03-1980:11.95
Re: extraction of data
by Anonymous Monk on May 14, 2014 at 13:59 UTC
    Maybe you should ask your instructor for assistance . . . mmmmm??

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1085995]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others sharing their wisdom with the Monastery: (7)
As of 2024-04-19 20:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found