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.


In reply to Re: Perl parse text file using hash by hv
in thread Perl parse text file using hash by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.