One option is to use DBD::AnyData to access the file with DBI and SQL. The script below works using data from the DATA section of a file, but if you substitute in the name of a file containging similar data, it will work just as well.
#!perl -w
use strict;
use DBI;
my @cols = qw( Name Phone Fax Address Email Info );
my @table = (\@cols);
for my $record(split /\s*%%%\n*/,join '',<DATA>) {
my @new_rec;
my @fields = split /\n/, $record;
for my $field(@fields) {
$field =~ s/^.*:\s*//;
push @new_rec, $field;
}
push @table, \@new_rec;
}
my $dbh = DBI->connect('dbi:AnyData:');
$dbh->ad_import('t','ARRAY',\@table);
my $sth = $dbh->prepare("
SELECT name,email FROM t WHERE address LIKE '%Anywhere%'
");
$sth->execute;
print $sth->dump_results;
__DATA__
Name: Robert Johnson
Phone: (555)555-1111
Fax: (555)555-1112
Address: 12345 Anywhere St.
Email: robertjohnson@someisp.com
Info: more info
%%%
Name: Robert Goor
Phone: (555)555-1113
Fax: (555)555-1114
Address: 12345 Somewhere St.
Email: robert@cs.someu.edu
Info: Some more info
%%%
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.