in reply to Perl parse text file using hash

Looking at the original code, a few things stand out:

Rewriting your program to show the data you got:

#!/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

Will - with your test data - result in:

-I-: Reading from config file: test.txt Albert apple /path/to/somewhere/a Albert jackfruit - Albert pineapple - Dex apple - Dex jackfruit /path/to/somewhere/d Dex pineapple - Jack apple /path/to/somewhere/c Jack jackfruit - Jack pineapple /path/to/somewhere/b

Enjoy, Have FUN! H.Merijn

Replies are listed 'Best First'.
Re^2: Perl parse text file using hash
by hv (Prior) on Dec 20, 2022 at 18:17 UTC

    The first argument to split is a regex unless you want paragraph mode (and you don't)

    That isn't what is happening here, this is a different special case documented in `perldoc -f split`:

    As another special case, "split" emulates the default beha +vior of the command line tool awk when the PATTERN is either omitt +ed or a string composed of a single space character (such as ' ' o +r "\x20", but not e.g. "/ /"). In this case, any leading whi +tespace in EXPR is removed before splitting occurs, and the PATTER +N is instead treated as if it were "/\s+/"; in particular, this + means that any contiguous whitespace (not just a single space ch +aracter) is used as a separator. However, this special treatment ca +n be avoided by specifying the pattern "/ /" instead of the str +ing " ", thereby allowing only a single space character to be a sep +arator. In earlier Perls this special case was restricted to the u +se of a plain " " as the pattern argument to split; in Perl 5.18.0 + and later this special case is triggered by any expression whi +ch evaluates to the simple string " ". If omitted, PATTERN defaults to a single space, " ", trigg +ering the previously described awk emulation.

    The references to awk are probably not very helpful these days, and probably discourage people from reading the rest and using this useful construct.