#!/usr/bin/perl use 5.014002; use warnings; my @fruits; my %info = parse ("test.txt"); # %info now holds: # { Albert => { # apple => { # path => '/path/to/somewhere/a' # } # }, # Dex => { # jackfruit => { # path => '/path/to/somewhere/d' # } # }, # Jack => { # apple => { # path => '/path/to/somewhere/c' # }, # pineapple => { # path => '/path/to/somewhere/b' # } # } # } foreach my $name (sort keys %info) { foreach my $fruit (sort @fruits) { printf "%-7s %-12s %s\n", $name, $fruit, $info{$name}{$fruit}{path} // "-"; } } sub parse { my $file = shift; my %info; -e $file or return; open my $fh, "<", $file or die "Can't open $file: $!\n"; say "-I-: Reading from config file: $file"; my %seen; while (<$fh>) { m/^\s*(?:#|\s*$)/ and next; my @fields = split m/\s+/ => $_; my ($name, $fruit, $p) = @fields; $seen{$fruit}++ or push @fruits => $fruit; $info{$name}{$fruit}{path} = $p; } close $fh; return %info; } # parse