#!/usr/bin/perl use strict; use warnings; my @input_array = qw(N1 N2 N3 N6 N7 B3 B4); #not fixed (user input) # I would recommend a "hash of array" # each "name" in the hash table like N1 or B3 points to a simple array # the first member of this array is CC0 and the second member is CC1 my %hash; foreach my $inputKey (@input_array) { @{$hash{$inputKey}} = (1,"xyz"); # each key of the hash has an # array with 2 members } foreach my $key (sort keys %hash) { my ($CC0, $CC1) = @{$hash{$key}}; print "key=$key; CC0=$CC0; CC1=$CC1\n"; } __END__ prints: key=B3; CC0=1; CC1=xyz key=B4; CC0=1; CC1=xyz key=N1; CC0=1; CC1=xyz key=N2; CC0=1; CC1=xyz key=N3; CC0=1; CC1=xyz key=N6; CC0=1; CC1=xyz key=N7; CC0=1; CC1=xyz