"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

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



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.