"I wouldn't suggest using pack and unpack to build a database."
I would. In fact, that is historically exactly what they are for. On the other hand, I wouldn't suggest building your own database when there are perfectly good ones out there so it is rather moot. =)
If your needs are lightweight and the data sets are small you should likely try DBD::CSV or even just something like:
#!/usr/bin perl -w use strict; use Data::Dumper; my @LoL = ( ["aa", 1, 2, 3, "Hi Mom!"], ["bb", 2, 3, 5, "No soup for you!"], ["cc", 3, 1, 2, "Not me!"], ["def", 4, 4, 4, "Like gag me with a spoon!"], ); { #block for localizing open FOUT, ">./datafile"; local $\=$/; #Record separator set to input separator. local $"=$;; #Array interpolation separator set to subscript separat +or. print FOUT "@$_" foreach (@LoL); close FOUT; } my @newLoL; { #block for localizing open FIN, "<./datafile"; push (@newLoL, [split m/$;/]) while (<FIN>); close FIN; } print Dumper(\@LoL, \@newLoL);
As long as you don't want space efficiency, memory efficiency, power, or binary data, that way is blazingly fast. =P The field separator in your file will be "\034" and the record separator would be "\n" so those two chracters are verboten in the data.
Useless triva for you, chracters \034-\037, the ones between the ESC and the SPACE chracters were once for data separation. Perl (thanks to AWK) uses \034 aka "FS" as its field separator. This is in fact a horrendous error since the original meaning of the chracters is: File, Group, Record, and Unit Separator. We really "should" be using the Unit separator "\037" for breaking up fields but so many people do it wrong that it isn't worth the fight.
--
$you = new YOU;
honk() if $you->love(perl)
In reply to Re: (tye)Re: HOW TO CREATE A DATABASE FILE WITH FIELDS IN RECORDS
by extremely
in thread HOW TO CREATE A DATABASE FILE WITH FIELDS IN RECORDS
by Anonymous Monk
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |