my %hash = map { /(ID)(name)(email)(title)(date)(...)/ && do { ($1, [$2, $3, $4, $5, ...]) } } @lines; # or, equivalently: my %hash = map { ($1, [$2, $3, $4, $5, ...]) if(/(ID)(name)(email)(title)(date)(...)/ } @lines;