Hi to all ,
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 .

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 = {};
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
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
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";




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


Reply via email to