$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
-
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.
|