#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my %hash; my @array; while (<>) { chomp; next unless /^node*/; # skip lines that do not start with string node my @tmp = split / /; push @array, $tmp[1]; } continue { close ARGV if eof; $hash{$ARGV} = [ @array ]; } print Dumper \%hash; __END__ $ perl test.pl in.txt in2.txt $VAR1 = { 'in2.txt' => [ '0', '0', '0', '8', '8', '8', '250', '250', '250', '251', '251', '251', '252', '252', '252', '253', '0', '0', '0', '8', '8', '8', '250', '250', '250', '251', '251', '251', '252', '252', '252', '253' ], 'in.txt' => [ '0', '0', '0', '8', '8', '8', '250', '250', '250', '251', '251', '251', '252', '252', '252', '253' ] };