Stanislav Nedelchev wrote:
> Hi to all

Hello,

> 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};
> }

Your problem is that %hashrecord is declared outside the while loop so the
reference always points to the same hash every time.  And you can't declare
the hash inside the while loop because that would just create a new hash for
every line in the file so you couldn't accumulate the records.  You need to do
something like this (UNTESTED):

while ( <FILE> ) {
    if ( /--=NewRecord=--/ ) {
        push @data, {};
        }
    else {
        chomp;
        my ( $fname, $fvalue ) = split /=/, $_, 2;
        $data[ -1 ]{ $fname } = $fvalue;
        }
    }



John
-- 
Perl isn't a toolbox, but a small machine shop where you can special-order
certain sorts of tools at low cost and in short order.       -- Larry Wall

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
http://learn.perl.org/


Reply via email to