On Fri, Dec 28, 2001 at 01:11:15PM +0100, Eric Van Buggenhaut wrote:
>       print Dumper($virtula1);
> 
> I spent some time try to understand why it was failing ;)

oops. yeah.  i should have cut-and-pasted the script i got working in
/tmp instead of what i originally typed in the message.  there were a
few obvious errors in the message.  but the point was to be provide
illustration of how to go about it, not an exact solution for your
problem.  i.e. method, not detail.


---cut here---
#! /usr/bin/perl -w

use Data::Dumper ;
use strict ;

my @fields = qw(hostname username password port database attributes 
                connect driver dbhost) ;

my $virtual1 = {} ;

while (<>) {
        chomp ;
        s/#.*// ;
        s/^\s*|\s*$//g ;   # strip leading & trailing spaces
        next if (/^$/) ;
        my @line = split /\|/ ;
        foreach(1..8) {  # loop from $fields[1]..$fields[8]
                $virtual1->{$line[0]}->{$fields[$_]} = $line[$_] ;
        } ;
} ;

print Dumper($virtual1) ;
---cut here---

from the (modified) input you provided, this produces output like this:


---cut here---
$VAR1 = {
          'personales' => {
                            'driver' => 'mysql',
                            'username' => 'root',
                            'attributes' => '{}',
                            'database' => 'acs',
                            'port' => '',
                            'password' => 'op.re,13',
                            'dbhost' => 'localhost',
                            'connect' => 
'DBI:mysql:database=PaginasPersonales:host=localhost'
                          },
          'acs' => {
                     'driver' => 'mysql',
                     'username' => 'root',
                     'attributes' => '{}',
                     'database' => 'acs',
                     'port' => '',
                     'password' => 'op.re,13',
                     'dbhost' => 'localhost',
                     'connect' => 'DBI:mysql:database=acs:host=localhost'
                   }
        };
---cut here---

which is pretty much the structure you wanted.



other comments:

i still think you should use a field separator which isn't in the field
contents - much simpler, and far less prone to error.

there's also no need to have quotes (`) around the connect string -
you'll only have to strip them off before using it.

also, why have the connect string at all when it can be built up from
the details provided in the other fields?  it seems to me that the
fields you need are:

 username
 dbi_driver
 attributes
 db_name
 db_host
 db_port
 db_user
 db_password

the connect string can be built up like so:

        $connect = "DBI:$driver:database=$db_name:host=$db_host" ;

(using db_port, db_user, and db_password as well if required)

craig

-- 
craig sanders <[EMAIL PROTECTED]>

Fabricati Diem, PVNC.
 -- motto of the Ankh-Morpork City Watch


Reply via email to