Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Re: Building an arbitrary-depth, multi-level hash

by GrandFather (Saint)
on Feb 28, 2009 at 22:16 UTC ( [id://747218]=note: print w/replies, xml ) Need Help??


in reply to Building an arbitrary-depth, multi-level hash

Rather than describing a solution that you can't find, how about describing the problem you are trying to solve. Not the little piece you are having trouble with, but the whole thing.

You seem to be scanning files and extracting some information from them, but you only give us a portion of a single file as sample data and don't show us enough to demonstrate the problem you are having.

You may find it helps to create a stand alone script to demonstrate the problem. The following starting point may help:

use strict; use warnings; use Data::Dump::Streamer; my %files; $files{wibble} = <<'END_FILE'; Name=foo Icon=bar Categories=a;b;c END_FILE $files{floop} = <<'END_FILE'; Name=baz Icon=boo Categories=x;y;z END_FILE my %collection; for my $file (keys %files) { open my $inFile, '<', \$files{$file} or die "Can't open $file: $!" +; my %collect; while (my $line = <$inFile>){ chomp $file; my ($var, $value) = split '=', $line, 2; $collect{$var} = $value if defined $value; } $collect{categories} ||= "Misc"; $collect{icon} ||= "-"; $collection{$file} = \%collect; } Dump \%collection;

Prints:

$HASH1 = { floop => { Categories => "x;y;z\n", categories => 'Misc', Icon => "boo\n", icon => '-', Name => "baz\n" }, wibble => { Categories => "a;b;c\n", categories => 'Misc', Icon => "bar\n", icon => '-', Name => "foo\n" } };

True laziness is hard work

Replies are listed 'Best First'.
Re^2: Building an arbitrary-depth, multi-level hash
by Anonymous Monk on Feb 28, 2009 at 23:14 UTC
    Rather than describing a solution that you can't find, how about describing the problem you are trying to solve. Not the little piece you are having trouble with, but the whole thing.
    Well, OK. Since I might run into problems at the far end of it as well, I guess it makes sense to do that. I'm trying to grab a list of all apps registered in the GNOME applications list and convert it to a menu format used by several other window managers. The first format, or at least the parts of it that mean anything, you've already seen (and used in your script); the second one looks like this:
    menu Applications folder { menu Editors folder { prog vim vim vim prog Lyx emacs lyx } menu "Mail Agents" folder { prog Mutt mutt xterm -e mutt } menu "WWW Browsers" folder { prog Mozilla mozilla mozilla prog Firefox /usr/share/pixmaps/firefox.png firefox prog Seamonkey /usr/share/pixmaps/firefox.png seamonkey prog w3m lynx xterm -e w3m prog Links lynx xterm -e links } menu Graphics folder { prog Gimp gimp gimp } menu Development folder { prog ddd ddd ddd } prog "Acrobat Reader" pdf acroread prog "DVI Previewer" xdvi xdvi } menu Games folder { prog "Koules for X" koules xkoules -f prog Xboing xboing xboing prog Xboard xboard xboard prog XGalaga xgalaga xgal prog XDemineur xdemineur xdemineur prog ppracer /usr/share/pixmaps/ppracer.xpm /usr/games/ppracer prog Arena arena openarena } ...
    Please note that folders (e.g., Applications) can contain other folders (e.g., Editors). Any folder can also contain program entries, which are structured as
    prog "Program name" icon.ext executable_name
    where the program name must be quoted if it contains whitespace, the icon name must be either a filename or a dash (i.e., no icon), and the executable name can be a PATHed program, an absolute path to a program, or a shell construct to be executed. The menu can contain a few other things, but I'm not concerned with any of those at the moment. The overall problem I'm having is creating the folder/program structure that I want reflected in the menus. That's a chain of categories that terminates in one or more programs belonging in that category - i.e.
    menu "Games" folder { menu "GNOME" folder { menu "Action" folder { prog "Arena" arena.png openarena prog "ppracer" /usr/share/pixmaps/ppracer.xpm /usr/game +s/ppracer } } menu "KDE" { ...
    If you can help with the overall problem, I'd appreciate it.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://747218]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (2)
As of 2024-04-26 03:10 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found