Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

reading flat file

by raj000 (Initiate)
on Oct 02, 2011 at 23:35 UTC ( #929209=perlquestion: print w/ replies, xml ) Need Help??
raj000 has asked for the wisdom of the Perl Monks concerning the following question:

Hi experts,

Is there any way we can read the flat file by field names. For example I have a flat file like

name=AAAAAAAA DOB=10101970

name=BBBBBBBB DOB=10101971

name=CCCCCCCC DOB=10101972

Is there a way to refer these values by name, DOB after i read the record in the PERL program?

Thank you very much in advance

Comment on reading flat file
Re: reading flat file
by Anonymous Monk on Oct 02, 2011 at 23:45 UTC

    Is there a way to refer these values by name, DOB after i read the record in the PERL program?

    Show your program (How do I post a question effectively?)

    Or see hash_reference from file

    Or see

    #!/usr/bin/perl -- use strict; use warnings; use Data::Dumper; print Dumper( kvkv( 'asdf=asdf blah=blah ra=rah' ) ); sub kvkv { my %foo; while( $_[0]=~ /([^=\s]+)=([^=\s]+)\s*/g ){ $foo{$1}=$2; } return \%foo; } __END__ $VAR1 = { 'blah' => 'blah', 'ra' => 'rah', 'asdf' => 'asdf' };

      Thank you for the reply. I am new to PERL, So let me ask you this dump question I want to read it from the file. How will i give my file name. Is it with OPEN(file1, location) statement?

        If you know nothing about Perl, you can read the documentation on your machine, if you have a Mac or Linux machine, by typing 'perldoc perl' to get a list of all the documention available for you to read. or surf your browser to CPAN.org and click on "Learn Perl" under "Getting Started". Lots of useful info there, including material about writing your first program, or click on "Perl documentation" for the stuff I mentioned before.

        As Occam said: Entia non sunt multiplicanda praeter necessitatem.

        If you're new to Perl, which programming language are you proficient in? What resource are you using to aid your learning of Perl? And what have you tried so far?

        If you're new to Perl, but competent in some other language, sometimes it helps for us to be aware of that so that we know how to answer in a way that will make the most sense in consideration of your background. If your background is that you don't know the first thing about programming, that would be helpful for us to know as well. Everyone starts somewhere, and there's no shame in that. We're also fairly familiar with many of the learning resources commonly available on the subject. So if we know where you're coming from in that regard as well, we may be able to sort of fill in the gaps or refer you to portions of that resource that might provide additional information. But like I said, everyone starts somewhere, and we all learned Perl somewhere along the way. There's nothing wrong with that.

        Not everyone duplicate-posts their original question, and then one-ups that by duplicate-posting their followups as well. Some do though, so you're not alone. But in the absence of additional background information, that sort of thing will probably skew our assessment as to what level of computer literacy our answers should target.

        One easy way to read from a flat file is to use the open function (described in better detail perlintro, open, and perlopentut). Next one would use the <> (diamond operator), as described in perlintro and perlop. This combined with a while(){} loop (perlintro and perlsyn) is almost all that's needed for the common idiom:

        open my $file_handle, '<', 'filename.txt' or die $!; while( <$file_handle> ) { chomp; # do something with the line held in $_ } close $file_handle;

        my, die, and chomp may also be helpful to your learning process.


        Dave

Re: reading flat file
by tospo (Hermit) on Oct 03, 2011 at 12:35 UTC
    Welcome to Perl!
    This is easily achievd in Perl and the script to do this won't be very long but there are a few things you need to learn. These are:
    • open a file and read line by line (a solution was shown already in a previous reply)
    • use regular expressions to get the named fields (tag/value pairs) out of the line of text. You could also use "split" to first split on spaces to get a list of tag/value pairs and then split on the "=" for each one to get the tag and the value.
    • read the data into a hash - find out what hashes are and how to populate them and read from them.
    Have a go at a short script. Doesn't matter if it doesn't work at first. Post it here if it doesn't work and you will receive more help. Good luck!

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2014-08-29 08:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (277 votes), past polls