A piece of perl code

578阅读 0评论2010-04-09 yk325
分类:

Perl

use Data::Dumper;

my %hash;

while(<DATA>){
    next if /^$/;
    my @array = split("<--",$_);
    
    my $array_length = $#array + 1;

    
    while($#array != 0){

        my $key = pop @array;
        if(! exists($hash{$key})){
            $hash{$key} = [ $array[-1] ];
        } else {
            next if ( grep { $_ eq $array[-1]} @{ $hash{$key} } );#if the item already exists in the array, skip it

            push @{ $hash{$key} }, $array[-1]; # put the item into the array if it does not exist in the array

        }
    }
}

print OFH Dumper(\%hash);


__DATA__

tt1 <-- tt2 <-- tt3 <-- tt4
tt1 <-- tt2 <-- tt3 <-- tt4


上一篇:关于volatile和restrict关键字
下一篇:进程的内存使用解析