John W. Krahn wrote:
> 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

>> 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?
Unique means that this file is 12Mb and on every row there is for example

here is the example
-----------------Here is example data first row-----------------------
skill_begin     skill_name = [s_hate11]                 skill_id = [s_hate11]   
level = 1       
operate_type = A1       magic_level = 22                                        
        skill_end
--------------------------second row-----------------------------------
skill_begin     skill_name = [s_hate12]                 skill_id = 28           
level = 2       
operate_type = A1       magic_level = 23                effect = {}     
is_magic = 0    skill_end
------------------Third row-------------------------------------
skill_begin     skill_name = [s_elemental_heal_power_1] skill_id = 3    
reuse_delay = 10        attribute = attr_none   effect_point = 780              
                                skill_end
--------------------------------------------------------------------------------------
.....etc

Here is example SQL

create table {
skill_name,
skill_id,
level,
operate_type,
magic_level,
effect,
is_magic,
reuse_delay,
attribute,
effect_point
}

Here is example insert based ot this data.
insert into table values("[s_hate11]","","28","[s_hate11]","1","A1","22");
insert into tables valuse("[s_hate12]","28","2","A1","23","{}","0")
insert into tables
values("[s_elemental_heal_power_1]","3","10","attr_none","780")

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


Reply via email to