my $ERRORCOUNT=0; my %HASH; my %OTHERHASH; ... foreach my $LINE (@LINES) { # the sub-routine works and is returning the desired output my ($vlan, $mac, $int, $counter) = linesifter($LINE); if ($error eq 1) { ERRORCOUNT++; } else { my @VLANS, @MACS, @INTS); push $HASH{$MACS[$mac]}, [$VLANS[$vlan], [$INTS[$int]]; } # here will follow similar code from another file # compare $HASH[$MACS[keymatch]] to $OTHERHASH[$MACS[$keymatch]] # then do *magic* and line up all items that are referenced by matching MAC key in both files onto one line. Store in a file. sub linesifter { #... #...omitted # various regex'es for grep'ing data #... return ($vlan, $mac, $int, $error); }