$rd is the record delimiter and $fd the field delimiter. Both must not be found in either keys or strings. I've used \n and \t for simplicity, but special characters or character sequences are probably smarter.
my $rd = "\n";
my $fd = "\t";
my %db;
$db{worda}[0] = "stringa";
$db{worda}[1] = "stringb";
$db{worda}[2] = "stringc";
$db{wordb}[0] = "stringd";
$db{wordb}[1] = "stringe";
$db{wordc}[0] = "stringf";
$db{wordd}[0] = "stringg";
$db{worde}[1] = "stringh";
$db{worde}[2] = "stringi";
$db{worde}[3] = "stringj";
for (keys %db) {
print join $fd, $_, @{$db{$_}};
print $rd;
}
And to reverse the generated data:
my $rd = "\n";
my $fd = "\t";
my (%db, $p, $i);
$/ = $rd;
while (<DATA>) {
chomp; ($_, @_) = split $fd;
$db{$_} = [@_];
}
for (sort keys %db) {
$p = $db{$_};
for ($i = 0; $i <= $#$p; $i++) {
print "\$db{$_}[$i] = \"$p->[$i]\";\n";
}
}
__DATA__
wordc stringf
wordd stringg
worde stringh stringi stringj
worda stringa stringb stringc
wordb stringd stringe
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.