in reply to Perl parse text file using hash

I'm not sure why some are reacting negatively, I reckon you've made a pretty good first post by showing your code and the example input - any expert will see immediately that it won't compile, so I'm not worried that you didn't tell us that. The code is also written very cleanly and mostly has the right ideas.

The important thing you didn't tell us was what output you actually want. It appears you want some output for each fruit, but what output do you want for "apple", which appears twice? And is the intention here to solve a specific problem, or to have a framework that you can use to get other types of output that may be filtered by the name or the path?

If you are looking for a general framework, then I suggest creating a data structure for each line containing all the data for that line, then linking to that in various ways. That might look something like this:

use strict; use warnings; my $file = "/path/to/config/config.txt"; my(%by_name, %by_fruit, %by_path); for my $data (parse_file($file)) { # Create lookups for each data item returned. # We assume any data can appear more than once, so we # store an array(ref) of data structures for each match. push @{ $by_name{$data->{name}} }, $data; push @{ $by_fruit{$data->{fruit}} }, $data; push @{ $by_path{$data->{path}} }, $data; } # now let us show all the paths by fruit: for my $fruit (sort keys %by_fruit) { print "Fruit $fruit:\n"; # $by_fruit{$fruit} is now an arrayref of data structures # matching 'fruit eq $fruit' for my $data (@{ $by_fruit{$fruit} }) { print " $data->{path}\n"; } } # Return a list of data structures, one for each line of # the specified file that contains data. # Blank lines and comment lines are ignored. sub parse_file { my $file = shift; open(my $fh, "<", $file) or die "Can't open < $file: $!"; return map { if (/^#/ || /^\s*$/) { (); # no data on this line } else { my($name, $fruit, $path) = split ' ', $_; # Return a data structure as a hash reference { name => $name, fruit => $fruit, path => $path, }; } } <$fh>; # Note that $file is automatically closed when the variable # goes out of scope, on return from this function. }

Note that if your needs become more complex, this data structure could easily be upgraded to an object as part of an object-oriented solution.