A
Adam
I'm writing a hash (string keys, string values) to a text file (by STDOUT)
for reading later, and I decided on the following format:
key|value|
another key|another value|
to make the file clearly human-readable (the values and keys can contain
spaces). I've also provided for escaping ``|'' and ``\'' in the data with
``\|'' and ``\\'' respectively.
Here's the output routine
foreach $key (keys(%table) ) {
$value = $table{$key} ;
$key =~ s/\\/\\\\/g ;
$key =~ s/\|/\\\|/g ;
$value =~ s/\\/\\\\/g ;
$value =~ s/\|/\\\|/g ;
print($key . "|" . $value . "|\n") ;
and here's the input routine
while ($line = <>) {
chomp($line) ;
$line =~ /^(.*([^\\]|\\\\))\|(.*)\|$/ ;
$key = $1 ;
$value = $3 ;
$key =~ s/\\\|/\|/g ;
$key =~ s/\\\\/\\/g ;
$value =~ s/\\\|/\|/g ;
$value =~ s/\\\\/\\/g ;
$table{$key} = $value ;
}
They seem to work, but I'm not sure how efficient they are (in particular
I have doubts about the regexp), so I'd appreciate any suggestions for
improvement.
I've also just noticed that the input routine would not correctly handle a
line like this:
blah\\|blahblah|
What's the best way to reverse the escapes?
for reading later, and I decided on the following format:
key|value|
another key|another value|
to make the file clearly human-readable (the values and keys can contain
spaces). I've also provided for escaping ``|'' and ``\'' in the data with
``\|'' and ``\\'' respectively.
Here's the output routine
foreach $key (keys(%table) ) {
$value = $table{$key} ;
$key =~ s/\\/\\\\/g ;
$key =~ s/\|/\\\|/g ;
$value =~ s/\\/\\\\/g ;
$value =~ s/\|/\\\|/g ;
print($key . "|" . $value . "|\n") ;
and here's the input routine
while ($line = <>) {
chomp($line) ;
$line =~ /^(.*([^\\]|\\\\))\|(.*)\|$/ ;
$key = $1 ;
$value = $3 ;
$key =~ s/\\\|/\|/g ;
$key =~ s/\\\\/\\/g ;
$value =~ s/\\\|/\|/g ;
$value =~ s/\\\\/\\/g ;
$table{$key} = $value ;
}
They seem to work, but I'm not sure how efficient they are (in particular
I have doubts about the regexp), so I'd appreciate any suggestions for
improvement.
I've also just noticed that the input routine would not correctly handle a
line like this:
blah\\|blahblah|
What's the best way to reverse the escapes?