Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
Back in the days when I didn't have access to databases, I used to write everything to flatfiles. This sub opens a delimited CSV file and takes the column names from the first line (which gets choped off) then it rolls through using the 1st row as a "primary key".

An example of what a database file might be (tab delimited):
PEOPLE CARS CAR_COLORS AGES John Volks avocado 26 Mira Fiat (yuck) black 25 Mom Twingo cherry 50
I haven't used it for a long time, but maybe someone has comments or even has a use for it...
my @columns = OpenFlatFile('TEST') or warn('file missing, unaccessible or empty'); foreach (keys %{$columns[0]}) { # might as well have said 'keys %PEOPLE' print "$PEOPLE{$_} drives a $CAR_COLORS{$_} $CARS{$_} and is $AGES{$ +_} years old.\n" } sub OpenFlatFile { my ($datfile) = $_[0]; my $delimiter = "\t"; my $val; open(FILE,$datfile) or return(0); my @tmp = <FILE>; close(FILE); my $head = shift(@tmp); chop $head; @{$datfile} = split($delimiter,$head); foreach (@tmp) { my $x = 0; chop; my @line = split($delimiter,$_); foreach $val (@{$datfile}) { if ((! defined ${$val}{$line[0]}) && ($line[$x] ne '')) { ${$val}{$line[0]} = $line[$x]; } ++$x; } } return(@{$datfile}); }

In reply to Open Flat File by BBQ

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chanting in the Monastery: (7)
As of 2024-04-19 06:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found