Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

extracting data from file

by shivu126 (Initiate)
on Nov 15, 2012 at 11:39 UTC ( #1003998=perlquestion: print w/replies, xml ) Need Help??
shivu126 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all, i am new for the perl programming and need a solution for the following question please help me out. one file contains date,goods,quantity 12-11-2006, books, 100 10-11-2006, bag, 10 10-12-2006, pen, 15 when we give input as date : 12-11-2006 it should give output as books,100

Replies are listed 'Best First'.
Re: extracting data from file
by roboticus (Chancellor) on Nov 15, 2012 at 11:51 UTC


    I'd suggest reading perldoc perlintro and perldoc perlfunc, especially the sections on string handling and regular expressions--split looks like a reasonable choice for this task.


    When your only tool is a hammer, all problems look like your thumb.

Re: extracting data from file
by space_monk (Chaplain) on Nov 15, 2012 at 14:30 UTC
    Whilst the answers using split are reasonable solutions, your file is really a CSV (Comma Separated Value) file, so the "professional" answer is to use a module such as Text::CSV to do the work for you.
    A Monk aims to give answers to those who have none, and to learn from those who know more.
Re: extracting data from file
by marto (Bishop) on Nov 15, 2012 at 11:42 UTC

    Welcome to Perl monks. You can make your input and output easier to read by using code tags, this is described in How do I post a question effectively?. What have you tried so far, and how did it fail?

Re: extracting data from file
by 2teez (Priest) on Nov 15, 2012 at 12:24 UTC

    Hi shivu126,
    Perl hash to the rescue, something like this:

    use warnings; use strict; my %data_arrang; while (<DATA>) { my @data = split /, / => $_, 2; push @{ $data_arrang{ shift(@data) } }, @data; } print "Enter your date: "; chomp( my $date = <STDIN> ); if ( exists $data_arrang{$date} ) { print "goods,quality : ", @{ $data_arrang{$date} }; } else { print "Date entered didn't exist\n" } __DATA__ 12-11-2006, books, 100 10-11-2006, bag, 10 10-12-2006, pen, 15

    You may want know more about Hash in Perl, split, perldsc
    If you tell me, I'll forget.
    If you show me, I'll remember.
    if you involve me, I'll understand.
    --- Author unknown to me
Re: extracting data from file
by Anonymous Monk on Nov 16, 2012 at 03:12 UTC

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1003998]
Approved by marto
[thepkd]: Yes this is my first time in cb. Trying to work my way around.
[JohnCub]: "A ref too far" would make a good title for anyone's perl memoirs
[thepkd]: Anyways it works with $aohoaoh[0]->{'foo '}[0]->{'bar'}
[GotToBTru]: "I did it my $way"
[JohnCub]: I hope $way isn't a hashref. ;)
[GotToBTru]: many times i've been told I made a real hash of thiings
[GotToBTru]: so .. perhaps it should b e

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (10)
As of 2016-12-06 13:48 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (105 votes). Check out past polls.