Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Flat file DB

by tachyon (Chancellor)
on Aug 30, 2001 at 01:58 UTC ( [id://108924]=note: print w/replies, xml ) Need Help??


in reply to Flat file DB

Here is is in a nutshell. This example reads from the DATA filehandle. By default this is the stuff after __DATA__ at the end of the program but if you open $file onto it it will be the contents of that file. To do this just uncomment the # open DATA, $file....line. If you do a Super Search for "flatfile pipe delimited database" you will find heaps of stuff on this. If you have other problems supply your problem code and some sample data. Maybe have a look at New Monks if you want to good rundown on the Monastery and Perl all on one page.

#!/usr/bin/perl -w use strict; my $file = 'c:/data.txt'; my @data; my $line_cnt = -1; # get data in a 2D array @data[$record][$field] # open DATA, $file or die "Oops, Perl says $!\n"; while (<DATA>) { chomp; next unless $_; my @fields = split /\|/, $_; push @data, \@fields; $line_cnt++; } close DATA; # access data in our 2D array, note offsets are from 0 not 1 print "Line 2 element 3 is ", $data[1][2], "\n\n"; # dump all the data as an (indented :-) HTML table print "<table border='1'>\n"; for my $line ( 0 .. $line_cnt ) { print " <tr>\n"; my @fields = @{$data[$line]}; for my $field ( 0 .. $#fields ) { print " <td>$fields[$field]</td>\n"; } print " </tr>\n"; } print "</table>\n"; __DATA__ 1|A|B|C 2|D|E|F 3|G|H|I

cheers

tachyon

s&&rsenoyhcatreve&&&s&n.+t&"$'$`$\"$\&"&ee&&y&srve&&d&&print

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2024-04-25 14:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found