Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (3)
As of 2024-04-19 21:59 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found