O Monks! Cometh one bearing a great problem and seeking help... I've got a bunch of files that I'm reading with lines like this:
... Name=foo Icon=bar Categories=a;b;c ...
I read this in, parse it, and store it in a hash like so:
#!/usr/bin/perl -w use strict; chdir "/usr/share/applications"; my (%collect, $out); while (our $Fn = <*.desktop>){ chomp $Fn; open Fn or die "$Fn: $!\n"; while (<Fn>){ $collect{name} = "$1" if /^Name=(.*)$/; $collect{icon} = "$1" if /^Icon=(.*)$/; $collect{exec} = "$1" if /^Exec=(.*)$/; $collect{categories} = "$1" if /^Categories=(.*);?\s*$/; } $collect{categories} ||= "Misc"; $collect{icon} ||= "-"; for (qw/name exec categories/){ print "Bad value for '$Fn' ($_)\n" unless defined $collect{$_}; } my $val = qq#prog "$collect{name}" $collect{icon} $collect{exec}#; ### More code follows
This, however, is where things break down. What I want is to split the categories into a list, then create a hash that works like this:
# 'categories' entry is 'GNOME;Games;Action' push $out{GNOME}{Games}{Action}, $val; # 'GNOME;Games;Action;FPS' push $out{GNOME}{Games}{Action}{FPS}, $val; # ...and so on.
That's the part where I've been tearing out my hair: I've been trying to sensibly construct this thing, and... I'm completely stumped. I've been trying for hours non-stop. The most frustrating part is that I feel I should know how to do this: I've been working with Perl for years, and using hashes and refs, etc. - and my brain just refuses to give up the answer. What I have so far - this comes just below the first code chunk that I showed above - is this:
my @levels = split /;/, $collect{categories}; for (0 .. $#levels) { $out->{$levels[$_]} = {} unless exists $out->{$levels[$_]}; $out->{$levels[$_]} = $val if $_ == $#levels; } close Fn; } use Data::Dumper; print Dumper($out);
...which, of course, does not work. Please HELP! Thanks in advance.

In reply to Building an arbitrary-depth, multi-level 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.