如何从可变大小的数组初始化HoH

时间:2011-01-21 00:17:53

标签: arrays perl hash

我需要帮助找出如何适应$ hash {$ i}加载不同大小的@headers数组的情况。         用严格;         使用警告;

    my $file = "list.csv";

    open (FILE,"$file") || die "Can't open file: $!\n"; 
    my (@lines) = <FILE>; 
    close(FILE);

    my @headers = split(',',$lines[0]);#split up header line

    my %hash;
    for (my $i=1; $i < scalar(@lines); $i++)
    {
        my @strings = split(',',$lines[$i];

# NEED help here
        $hash{$i} = {
            $headers[0] => $strings[0],
            $headers[1] => $strings[0],
            $headers[2] => $strings[0],
            $headers[3] => $strings[0],
            $headers[4] => $strings[0],
            $headers[5] => $strings[0]
            };

    }

在标量(@headers)= 5,6,7等等的情况下,有没有办法在索引处加载哈希?是否存在类似......的程序化等价物。

$hash{$i} = {
        $headers[0] => $strings[0],
              ...
        $headers[n] => $strings[n]
        };

$hash{$i} = {@headers => @strings);

2 个答案:

答案 0 :(得分:5)

你想要的成语是:

@{ $hash{$i} }{ @headers } = @strings;

这称为slicing

鉴于您正在阅读CSV数据,您可能会查看某些CPAN模块,例如Text::CSV

答案 1 :(得分:1)

TIMTOWTDI

#!/usr/bin/perl

use strict;
use warnings;

my $file = "list.csv";

# Use lexical filehandles, not globals; use 3-arg open; don't quote filename
open ( my $fh, '<', $file ) or die "Can't open file: $!\n";
my( @lines ) = <$fh>;
close( $fh );

# split takes a regex; also, notice the shift
my @headers = split( /,/, shift @lines );

my %hash;

# Use perly for loops here
foreach my $i ( 0..$#lines )
# This works, too
#for my $i ( 0..$#lines )
{
    # split takes a regex
    my @strings = split( /,/, $lines[$i] );

    # One way (probably best)
    @{ $hash{$i} }{ @headers } = @strings;
    # Another way
    #$hash{$i} = { map { $headers[$_] => $strings[$_] } ( 0 .. $#strings ) };
    # Another way
    #$hash{$i}{ $headers[$_] } = $strings[$_] = for(0..$#strings);

}

#use Data::Dumper;
#print Dumper \%hash;

但是,是的,使用Text::CSV(或更快的Text::CSV_XS)会比尝试自己手动拆分CSV更好(如果有空格会怎么样?如果字段和/或标题被引用?这是一个已解决的问题。)