On 4/10/07, Stanislav Nedelchev <[EMAIL PROTECTED]> wrote:
Hi to all
Here is my problem . I'm trying to export this data to SQL database .
Here is example data.Don't pay attention on encoding. Every new record
is starting --=NewRecord=--
delimiter between fields and data is = .
Contact=АГППМП-СЪНИМЕД-ООД
Manager=
Region=РЗОК Бургас
Municipality= НЕСЕБЪР
Settlement=ГР.НЕСЕБЪР
Address=ул.Рибарска 11 каб.1-2
Phone=43058
Name=Людмил Николов Стаменов - Общопрактикуващ лекар
--=NewRecord=--
Contact=АИППМП д-р Р.ДимитровЕООД
Manager=
Region=РЗОК Бургас
Municipality= БУРГАС
Settlement=ГР.БУРГАС
Address=ул.Хан Крум №28 ет.2 ст.209
Phone=
Name=Румен Николаев Димитров - Общопрактикуващ лекар
I was try to adapt some code that i see here and some people are helped
me with other things.
Here is my code . It's seems that %hashrecord is not after firs asaing
to array is not changing anymore .
If i try to iniatilze the hash it's seem that i initialize the reference
and everything becomes empty.
Can anybody give advice how to fill this array of hashes and generate SQL.
use strict;
use warnings;
open FILE,"<TOTAL.txt" or die $!;
open OUT,">insert.sql" or die $!;
my @data;
my $sql;
my %hashrecord;
my %columns;
while(<FILE>){
if(! /--=NewRecord=--/){
my ($fname,$fvalue) = split/=/;
$hashrecord{$fname} = $fvalue;
}
if (/--=NewRecord=--/){push @data, \%hashrecord};
}
foreach my $rec (@data) {
$columns{$_}++ foreach keys %$rec;
}
foreach my $rec (@data) {
$sql = sprintf "insert into doctors (%s)\nvalues(%s)",
join(",", map qq( $_), keys %$rec),
join(",", map qq( '$_'), values %$rec);
print OUT $sql, "\n";
}
I certainly hope your code has indentation and that it was lost in the
copy&paste into the email. The short answer is you can't reuse
%hashrecord; you need a new one each time. To do this you will either
need to restructure your code heavily or make this less than optimal
change:
if (/--=NewRecord=--/) {
push @data, { %hashrecord }; #push a copy of %hashrecord onto the array
%hashrecord = (); #clear %hashrecord for the next record
}
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/