How do I initialize HoH from arrays of variable size
I need help figuring out how to accomodate situations in which $hash {$i} is loaded with @headers array of different sizes. use strict; use warnings;
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],
开发者_C百科 $headers[5] => $strings[0]
};
}
Is there a way to load up hash at index for in situations when scalar(@headers)=5,6,7 ... etc? Is there a programatic equivalent for something like...
$hash{$i} = {
$headers[0] => $strings[0],
...
$headers[n] => $strings[n]
};
or
$hash{$i} = {@headers => @strings);
The idiom you want is:
@{ $hash{$i} }{ @headers } = @strings;
This is known as slicing.
Given that you're reading CSV data you might look at some of the CPAN modules out there for this sort of thing, Text::CSV, for example.
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;
But yes, using Text::CSV (or the faster Text::CSV_XS) would be even better than trying to manually split the CSV yourself (what happens if there are spaces? what happens if the fields and/or headers are quoted? It's a solved problem.)
精彩评论