$info{$name}{$fruit}{path} = $p; $path=$info{$name}{$fruit}{path}; $name=$info{name}{$fruit}{$path}; #### $ cat test_input.txt Albert apple /path/to/somewhere/a # next line is Jack pineapple /path/to/somewhere/b # next line is only space characters Jack apple /path/to/somewhere/c # Comments only work when # is first character! Dex jackfruit /path/to/somewhere/d # next line is blank #### $ cat -vet test_input.txt Albert apple /path/to/somewhere/a$ # next line is $ ^I^I$ Jack pineapple /path/to/somewhere/b$ # next line is only space characters$ $ Jack apple /path/to/somewhere/c$ # Comments only work when # is first character!$ Dex jackfruit /path/to/somewhere/d$ # next line is blank$ $ #### #!/usr/bin/env perl use strict; use warnings; use autodie; my $input_file = 'test_input.txt'; my $info = parse_input_file($input_file); #get $address for each line in input file # Assume "path", not "address". # Will only be for each "processed" line: # no comments or blanks. #get $name for each line in input file # Again, will only be for each "processed" line: # no comments or blanks. # You also seemed to want a list of fruits. my (@names, @fruits, @paths, %seen); for my $name (keys %$info) { push @names, $name; FRUIT: for my $fruit (keys %{$info->{$name}}) { push @paths, $info->{$name}{$fruit}; next FRUIT if $seen{$fruit}++; push @fruits, $fruit; } } # TODO - for demo only; remove for production use Data::Dump; print "Data extracted:\n"; dd $info; print "Names:\n"; dd \@names; print "Fruits:\n"; dd \@fruits; print "Paths:\n"; dd \@paths; sub parse_input_file { my ($file) = @_; my $info = {}; { open my $fh, '<', $file; while (<$fh>) { next if /^(?:#|\s*$)/; my ($name, $fruit, $path) = split; $info->{$name}{$fruit} = $path; } } return $info; } #### Data extracted: { Albert => { apple => "/path/to/somewhere/a" }, Dex => { jackfruit => "/path/to/somewhere/d" }, Jack => { apple => "/path/to/somewhere/c", pineapple => "/path/to/somewhere/b", }, } Names: ["Jack", "Dex", "Albert"] Fruits: ["apple", "pineapple", "jackfruit"] Paths: [ "/path/to/somewhere/c", "/path/to/somewhere/b", "/path/to/somewhere/d", "/path/to/somewhere/a", ]