Chinaunix首页 | 论坛 | 博客
  • 博客访问: 519671
  • 博文数量: 174
  • 博客积分: 4177
  • 博客等级: 上校
  • 技术积分: 1827
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-15 14:12
文章分类

全部博文(174)

文章存档

2018年(1)

2017年(1)

2013年(3)

2012年(9)

2010年(12)

2009年(5)

2008年(106)

2007年(37)

我的朋友

分类:

2010-04-09 19:12:45

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


阅读(504) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~