码迷,mamicode.com
首页 > 编程语言 > 详细

数组作为hash元素的时候如何push

时间:2015-05-11 19:56:24      阅读:104      评论:0      收藏:0      [点我收藏+]

标签:

#######################################################################
#   Copyright (C) 2015 All rights reserved.
#   
#   文件名称:p.pl
#   创 建 者:
#   创建日期:2015年05月11日
#   描    述:
#
#   备    注:
#
#########################################################################

use strict;
use warnings;
use Time::HiRes;
# please add your code here!
my $tmStarted=Time::HiRes::time;
if (2 != scalar @ARGV)
{
    &PrintUsage();
    exit 1;
}
=pod
    Subroutine(s);
=cut
sub PrintUsage
{
    print STDERR "perl program.pl [IN] ref_file [IN] input_file [OUT] output_file\n";
}
my $line = "";
my $linecount = 0;
my %hash = ();
open FIN, "<$ARGV[0]" or die "can not open inputfile:$!\n";
open FOUT, ">$ARGV[1]" or die "can not create outputfile :$!\n";
while(defined($line=<FIN>))
{
    chomp $line;
    my @vec = split /\t/, $line;
    my $size = scalar @vec;
    if ($size < 3)
    {
        print STDERR "$line\n";
    }
    if (not defined($hash{$vec[0]}))
    {
        my @vectemp = ();
        push @vectemp, $vec[2];
        $hash{$vec[0]} = \@vectemp;
    }
    else
    {
        push @{$hash{$vec[0]}}, $vec[2];
    }

}
close FIN;
foreach my $key (keys%hash)
{
    my @vec2 = @{$hash{$key}};
    my $size = scalar @vec2;
    my $part = "";
    for (my $i = 0; $i <$size; $i++)
    {
        $part.=$vec2[$i];
        if ($i < $size - 1)
        {
            $part.=",";
        }
    }
    print FOUT "$key\t$part\n";
}
close FOUT;
print STDERR "$0 has finished,congratulations!\n";
print STDERR "Time elapsed:".(Time::HiRes::time-$tmStarted)."\n";

 

数组作为hash元素的时候如何push

标签:

原文地址:http://www.cnblogs.com/finallyliuyu/p/4495320.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!