Yes. Take a look at DBD::CSV.
It lets you treat a standard comma-separated value file
as a SQL database and perform SQL statements on it.
If you do eventually outgrow DBD::CSV, though (and you
probably will if you do much database work), do take
another look at MySQL. It's really a very fast, pretty
simple database system, and I highly recommend it. | [reply] |
DBD::CSV is very nice. And since it works like any other
DBI driver, you won't have to completely
rewrite your code when you are ready to upgrade
to mySQL or some other RDBMS.
| [reply] |
As pointed, DBD::CSV is definitly a way to go, but if you all you want to do is simple stuff (and I mean simple), check out this node for a flatfile alternative.
(a bit of self-promotion never hurt anyone, right? :o))
#!/home/bbq/bin/perl
# Trust no1!
| [reply] |
Of course, if it is a really simple format and you don't need
to do anything very fancy, you could just do it manually (assuming
records are separated by blank lines, that "Name" is your main
key, and that "Name" appears before all other lines in each
record):
open FILE, "<file" or die "$!\n";
{ local $/="";
while ($rec=<FILE>) {
my @fields=split("\n", $rec);
my $key;
foreach (@fields) {
my ($index, $value)=split(/\s*:\s*/, $_, 2);
if ($index eq 'Name') {
$key=$index;
$db{$key}={};
} else {
$db{$key}->{$index}=$value;
}
}
}
close FILE;
}
This will leave the data in %db, indexed by Name, and each
element will be a hash reference with two elements called
Othername and postmaster.
--ZZamboni
| [reply] [d/l] |
Yikes...i keep forgetting about the lack of carrage returns
################################
sub ParseProfile {
my($filename) = @_;
open(PROFILE,$filename);
@PROFILE = <PROFILE>;
close(PROFILE);
my(%profile);
$i = 0;
while (@PROFILE$i ne "") {
@LINE = split(": ", @PROFILE$i);
$value = "@LINE1"."@LINE2"."@LINE3".
"@LINE4"."@LINE5";
@temp = split("\n", $value);
$value = join("", @temp);
$profile{@LINE[0]} = $value;
$i = $i + 1;
}
return %profile;
}
###############################
sub SaveProfile {
my($profile_path, %profile) = @_;
open(PROFILE,">$profile_path");
while (($key, $val) = each %profile) {
print PROFILE "$key: $val\n";
}
close(PROFILE);
}
###############################
| [reply] |
| [reply] |
| [reply] |