Multi Line string in flat file DB

They have: 850 posts

Joined: Jul 1999

You could seperate each entry by a |. Than when you need to use the database, just get the contents of the database and split it up.

Somthing like this

code:

local $/;
open(IN,"<file.here");
$database=<IN>;

while($database =~ m/(.+?)\|/sogi)
{
$entry = $1 . "<br>";
push(@arr,$entry);
}
Now each entry is an element in the array @arr.
[/code]

See the previous code working at http://dlo.net/~rob/cgi-bin/textdatabase.cgi 
the text file can be viewed at http://dlo.net/~rob/cgi-bin/textdatabase.txt 

Hope that helps..

------------------
Due to precipitation, for a few weeks K2 is bigger than Mount Everest. 

They have: 297 posts

Joined: Apr 1999

Hey,

whats the best way to save a multi line string in a flat file database.

Do you have to do something like:
s/+(\n)/%linebreak%/g
and then undo the process when reading the file or what would work better?

Thanks,

Malte

They have: 161 posts

Joined: Dec 1999

You can use a line (or record) delimiter other than "\n" in your database. The popular fortune program, for instance, uses %% between its "records". Here's a sample implementation:

code:

open FILE, "filename";
{
  local $/ = "\n%%\n";
  chomp($first = <FILE>);
}
close FILE;
[/code]

The chomp() function does NOT remove the trailing \n on a list of strings -- rather, it removes the trailing value of $/ on a list of strings (it is documented as such, but people think it just removes a trailing newline).

------------------
-- 
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve 

They have: 297 posts

Joined: Apr 1999

Would this work for a database which contains more than one multi-line string in multiple rows and columns?

Malte

They have: 297 posts

Joined: Apr 1999

Japhy, I read your article about this on your homepage, so everything is fine.

Thanks,

Malte

They have: 161 posts

Joined: Dec 1999

Ah, cool Malte. Check out the PerlMonth.com web site -- that's where that column is.

I'm thinking of writing an article specifically on modifying the way data is input and output with the $/, $\, $", and $, variables. Tentative title would be "Funking it Up with the Punctuation Variables" -- but something makes me think that'll have to be changed.

------------------
--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.