在我解析一个大的Foo::Bar.baz(p=val1, q=val2)
文件后,这是.sql
的打印输出:
%hashtable
这是Key:AS_LINR
Value:
Name:DS_LSNE_DDD_TS_A
Type:view
Parents:DM_LINE_END MINA_TI_GRP_V
:
%hash
我需要检查每个父母是否在 $hashtable{$name}="Name:$name
Type:$type
Parents:@parents"."\n\n
".
"----------------------------"
;
中作为key
存在。如果他我需要更新它并添加一个名为children的新文件:,我将作为一个值添加到字段children我第一次找到父母的名字。就像这个例子:
%hash
我需要为每个家长做这件事。我想通过向其添加新元素来更新哈希,如果哈希的密钥不存在,我必须创建一个哈希。 如果我必须更好地解释我必须做的事情,请在评论中提出。
这是我的perl代码:
Key:DM_LINE_END
Value:
Name:DS_LSNE_DDD_TS_A
Type:view
Children:AS_LINR
这是我需要解析的数据块的样子:
my $var=0;
my @joinparents=();
use warnings;
my %hashtable;
open(DATA,'<','NaViews.sql') or die "Error $!";
open(Writer,'>','ResultFile.txt') or die "Error $!";
open(Writer1,'>','AuxResult.txt') or die "Error $!";
my @create_cmds = ();
my $create_cmd = "";
READ_DATA : while (<DATA>) {
chop;
my $ln = $_;
$ln =~ s/^\s+//;
$ln =~ s/\s+$//;
next READ_DATA if($ln =~ /^\-\-/);
next READ_DATA if($ln =~ /^REM/);
if($create_cmd ne "") {
$create_cmd = $create_cmd." ".$ln;
}
if($ln =~ /^create/i) {
$create_cmd = $ln;
}
elsif($ln =~ /\;$/) {
push @create_cmds, $create_cmd;
$create_cmd = "";
}
}
close DATA;
my @views = ();
foreach my $create_cmd (@create_cmds) {
$create_cmd =~ s/\s+/ /;
$create_cmd =~ s/^\s+//;
$create_cmd =~ s/\s+$//;
my $name = get_view($create_cmd);
my $type = get_type($create_cmd);
my $content = substr($create_cmd, 0, -1);
my @parents =();#get_parents();
my @children = ();#get_children();
#------------------------------------------------------------------------
my @froms = split(/ from\s+/i, $create_cmd);
my @joins = split(/ join /i, $create_cmd);
#parcurge mai multe for in aceeasi structura
#FOR FROM
# body...
foreach my $i (1..@froms-1) {
#print Writer1 "$froms[$i]"."\n\n";
my $from = (split(/ where |select | left | left | right | as /i, $froms[$i])) [0];
$from=~s/^\s+//;
$from=~s/\(+//;
my @Spaces = split(/, | , /,$from);
foreach my $x (0..@Spaces-1) {
my $SpaceFrom = (split(/ /,$Spaces[$x])) [0];
$SpaceFrom=~s/;//;
$SpaceFrom=~s/\)+//;
#print Writer1 $SpaceFrom."\n\n";
push(@parents,$SpaceFrom);
# print "\n\n".$SpaceFrom."\n\n";
# print Writer "\n\n".$SpaceFrom."\n\n";
}
foreach my $x (1..@joins-1){
#print "$joins[$i]"."\n\n";
my $join = (split(/ on /i,$joins[$x])) [0];
my $joinspace = (split(/ /i,$joins[$x])) [0];
#print Writer "\n\n".$join."\n\n";
#print Writer1 $joinspace."\n\n";
#"$joinspace\n\n";
push(@parents,$joinspace);
print Writer1"\n\n".$parents[$_]."\n\n";
}
}
push @views, [$name, $type, $content, @parents, @children];
$hashtable{$name}="[0]Name:$name
[1]Type:$type
[2]Content:$content
[3]Parents:@parents"."\n\n
".
"----------------------------";
}
print Writer "Key:$_
Value:
$hashtable{$_}\n" foreach (keys%hashtable);
#------------------------------------------------------------------------------
print_views(\@views);
exit;
#------------------------------------------------------------------------------
sub get_view {
my $create_cmd = $_[0];
my $tmp = (split(/ view | trigger | table /i, $create_cmd))[1];
$tmp =~ s/^\s+//;
my $view = (split(/\s+/, $tmp))[0];
return $view;
}
#-----------------------------------------------------------------------------
sub get_type{
my $create_cmd = $_[0];
my $tmp = (split(/ replace /i, $create_cmd))[1];
$tmp =~ s/^\s+//;
my $view = (split(/\s+/, $tmp))[0];
return $view;
}
#-----------------------------------------------------------------------------
sub get_parents {
}
sub get_children {
}
get_children();
close Writer1;
close Writer;
答案 0 :(得分:1)
如果你想在%hashtable
中轻松找出父母的条目,你会发现如果你把数据存储为另一个哈希而不是像这样的一个巨大的字符串...... / p>
$hashtable{$name}={"Name" => $name, "Type" => $type, "Parents" => \@Parent};
然后你可以引用$hashtable{$key}->{"Parents"}
来获得一个数组引用,其中包含你可以像这样使用的数据条目的父级...
foreach my $parent (@{$hashtable{$key}->{"Parents"}})
{
if(defined($hashtable{$parent}))
{
# Parent exists in hashtable
}
else
{
# Parent does not exist in hashtable
}
}