Take the 2-minute tour ×

I have an array @fields containing names for the tab-separated values in $record (it may have been populated from record 0 of the file, for instance).

The order of the array entries corresponds to the order of the values in the record.

I want to populate %hash with field => value entries for each value in the record.

Is there a one-liner to do this? I can't figure out how to coordinate traversing the @fields and split("\t", $record) list concurrently.

Best I can come up with is

my %hash;
my @values = split("\t", $record);
for my $field (@fields) {
    my $value = shift @values;
    $hash{$field} = $value;

But I suspect there's a simpler way to do it.


share|improve this question
add comment

2 Answers

up vote 4 down vote accepted

There is -- it's the hash slice notation:

@values = split /\t/, $record;
@hash{@fields} = @values;


@hash{@fields} = split /\t/, $record;
share|improve this answer
Perfect - thanks. –  Chap Sep 17 '12 at 22:31
add comment

This works:

@hash{@fields} = @values;


my @fields = qw[one two three four];
my @values = (1, 2, 3, 4);

my %hash;
@hash{@fields} = @values;

use Data::Dumper;
print Dumper(\%hash);


$VAR1 = {
      'three' => 3,
      'one' => 1,
      'two' => 2,
      'four' => 4
share|improve this answer
add comment

Your Answer


