Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re: reading flat file

by Anonymous Monk
on Oct 02, 2011 at 23:45 UTC ( #929214=note: print w/replies, xml ) Need Help??

in reply to reading flat file

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' };

Replies are listed 'Best First'.
Re^2: reading flat file
by raj000 (Initiate) on Oct 03, 2011 at 01:19 UTC

    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'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.



        You're always graceful, even when approaching these types of issues, that's hard to do. Had to give you a pat on the back. :)

        "The reasonable man adapts himself to the world; the unreasonable one persists in trying to adapt the world to himself. Therefore all progress depends on the unreasonable man.." -- George Bernard Shaw

      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 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.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://929214]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (7)
As of 2017-04-24 02:07 GMT
Find Nodes?
    Voting Booth?
    I'm a fool:

    Results (433 votes). Check out past polls.