Cleavis
asked on
Delimited Text Database Help
Hi,
I currently use text files as my database. I don't know how to use a delimited text file.
I am currently using the cut command to pull out my fields but this is a pain.
Here is an example of how i would populate my variable $foo with information.
system ("cut -c16-24 $file1 > $file2");
open (FILE, $file2);
$foo = <FILE>;
close (FILE);
Would somebody please help me get started using a delimited database. I understand the concept i just don't know how to pull the information out.
Thanks
Cleavis
I currently use text files as my database. I don't know how to use a delimited text file.
I am currently using the cut command to pull out my fields but this is a pain.
Here is an example of how i would populate my variable $foo with information.
system ("cut -c16-24 $file1 > $file2");
open (FILE, $file2);
$foo = <FILE>;
close (FILE);
Would somebody please help me get started using a delimited database. I understand the concept i just don't know how to pull the information out.
Thanks
Cleavis
#!/usr/bin/perl
use strict;
my $file = 'file.txt';
open FILE, $file or die "Can not open $file $!\n";
while (<FILE>) {
my $foo = substr($_,15,9);
print "F $foo\n";
}
use strict;
my $file = 'file.txt';
open FILE, $file or die "Can not open $file $!\n";
while (<FILE>) {
my $foo = substr($_,15,9);
print "F $foo\n";
}
To move your data from text files to a database, you'll want to acquire the data from each of the files. To do this, you should use the glob() function to get a list of files in the directory.
Next, you'll want to select a database system. MySQL is a commonly used database, free for non-commercial use, and an excellent choice for a wide variety of needs. To interface this with Perl, you can use the DBI module. Documentation is available here: http://search.cpan.org/~timb/DBI-1.43/DBI.pm
Like Tintin said above, you can use substr() to perform the same operation as the UNIX cut command. To add the data to a database, use code similar to the following:
#!/usr/bin/perl
use strict;
use DBI; #use the DBI module
my $dsn = "DBI:mysql:database=db_dir ectconnect ;host=loca lhost;port =$portnumb er";
my $dbh = DBI->connect($dsn, 'username', 'passwd'); #connect to the database
my $file = 'file.txt';
open (FILE, $file) or die "Can not open $file $!\n";
while (<FILE>) {
my $foo = substr($_,15,9); #get the data from the file
$dbh->do("insert into tbl_tablename (column_name) values ('$foo')"); #execute a SQL command
}
$dbh->disconnect(); #disconnect from the database
Hope this helps.
Next, you'll want to select a database system. MySQL is a commonly used database, free for non-commercial use, and an excellent choice for a wide variety of needs. To interface this with Perl, you can use the DBI module. Documentation is available here: http://search.cpan.org/~timb/DBI-1.43/DBI.pm
Like Tintin said above, you can use substr() to perform the same operation as the UNIX cut command. To add the data to a database, use code similar to the following:
#!/usr/bin/perl
use strict;
use DBI; #use the DBI module
my $dsn = "DBI:mysql:database=db_dir
my $dbh = DBI->connect($dsn, 'username', 'passwd'); #connect to the database
my $file = 'file.txt';
open (FILE, $file) or die "Can not open $file $!\n";
while (<FILE>) {
my $foo = substr($_,15,9); #get the data from the file
$dbh->do("insert into tbl_tablename (column_name) values ('$foo')"); #execute a SQL command
}
$dbh->disconnect(); #disconnect from the database
Hope this helps.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks everybody.
Tom, that is an excellent answer.
I really appreciate the help.
Cleavis
Tom, that is an excellent answer.
I really appreciate the help.
Cleavis
while(<FILE>){
#for each line of the file perform an action
@fields = split(/\t/, $_); #where the file is a tab deliminated
# other opertaions on file contents here
}