Stanislav Nedelchev wrote:
> Hi to all ,

Hello,

> I'm trying to make one script that analyzes some data from a file.
> One guy from internet help me a lot .
> Here is how data is organized .
> Every row starts with skill_begin and it ends with skill_end.
> between those 2 words there is field_name = some_data and etc.
> Some rows has more field that other and sometimes there is no whitespace
> between field_name=some_data. I want remove white spaces and tabs one or
> more with only one tab and write it to file . Second this is most
> difficult i want to find unique field and create SLQ create and insert
> based on this data. But anyway we made it some how but we are still
> working on it. I will be very happy if somebody can improve the script
> or give me some advice how to do in in other way .
> Here you can find example data.
> http://rapidshare.com/files/21298750/skilldata.rar.html
> 
> and here is the script i was try to make some remarks for some new in
> Perl like I'm .

You should have these two lines at the start of your program:

use warnings;
use strict;


> if ( ! open OUT, ">outfile.txt")  { die "Cannot create outfile.txt: $!";}
> if ( ! open OUT1, ">sqlstmt.txt") { die "Cannot create sqlstmt.txt: $!";}
> if ( ! open IN, "skilldata.txt") { die "Cannot open skilldata.txt: $!";}
> $var = "varchar(45)";
> %hash = {};

That is an mistake and if you had warnings enabled you would have received a
warning something like:

Reference found where even-sized list expected at yourprogram line 7.


> while(<IN>)
> {
> s/\/\*.*\*\//\t/;                                #Remove Remarks
> s/(\S+)\=(\S+)/$1 \= $2/g;                       #Add Whitespace betwee
> = and data
> for($i=1;$i<=$#a;$i=$i+3) {                      #Loop for generating

@a is empty at this point so this loop will be skipped.

> Create statesment
>    if(!$hash{$a[$i]}) {                          #filing hash keys from
> array key = "a"
>      if($a[$i]!~/skill_end/){
>         $hash{$a[$i]} = "a";
>         $create = $create."," . "\n" .$a[$i]." ".$var;
>      }
>    }
> }
> $create =~s/^,//; #Remove , in the begining.
> $stmt = "create table datafields($create)";
> for($j=3;$j<=$#a;$j=$j+3){                        #Loop for Generate

@a is still empty at this point so this loop will be skipped.


> insert statesment
> $ins = $ins.","." "."\"".$a[$j]."\"";             #taking 3 position
> value because data = value
> }
> $ins=~s/^,//;
> $sql = "insert into datafields values($ins)";
> if($sql =~/values\(\)/){} else {                  #if diffrent that
> initial var then wtite to file
>   print OUT1 "$sql\n";
>   $ins = "";
>   }
> @a = split /\s+/, $_;                            #split input data  on
> pieces based on whitespace.
>   foreach $a(@a){                                #Loop
>    @b = split/= /, $a;                           #split data on parts by =
>    #$b[0]=~s/ $//;                               #Removes empty space on
> the beginning
>    #$b[0]=~s/^ //;                               #Removes empty space on
> the end
>     if($b[0]!~/_begin|_end|\/\*|\*\//) {
>         print OUT "$b[0]\t";                #Write Data without
> skill_begin and skill_edn and add tabs delimeter.
>     }
> 
>   }
> print OUT "\n";                             #Put new line on every row
> }
> print OUT1"\n\n$stmt\n\n";

It is not clear what you mean by "find unique field and create SLQ create and
insert based on this data".  How do you determine the unique field, or do you
really mean unique record?

BTW, I used the following to "clean-up" the data.

$ perl -lne'
    s/\A.*?skill_begin\s*// &&
    s/\s*skill_end\s*\z// &&
    s!\s*/\*.*?\*/\s*!\t! &&
    tr/\t//s &&
    push @x, map [ split /\s*=\s*/, $_, 2 ], split /\t/
    }{
    print qq("$_->[0]" = "$_->[1]") for @x
' skilldata.txt




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