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});
}
-
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.
|