Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

AoHoAoH... am I delusional?

by jaydstein (Novice)
on Feb 08, 2012 at 00:30 UTC ( [id://952390]=perlquestion: print w/replies, xml ) Need Help??

jaydstein has asked for the wisdom of the Perl Monks concerning the following question:

I'm still getting acquainted with perl datastructures. I have a working structure for storing my data... but it is UGLY and BULKY. I'm sure there is a better way to do this... someone please slap me across the face and show me the error of my ways.

Note... the order in which the data is accessed is important! It is also my preference to preserve the key-value relationship between the data if at all possible.

my @fooFeatures = ( { 'Baked Goods' => [ { 'Cookies' => ['Chocolate Chip', 'Oreo'] }, ]}, { 'Fruits' => [ { 'Apples' => ['Fuji', 'Granny Smith'] }, { 'Oranges' => ['Tangerine', 'Navel'] }, ]}, ); for my $fooSet ( @fooFeatures ) { while (my ($key, $value) = each(%$fooSet)) { for my $hash ( @{$value} ) { while (my ($k, $v) = each(%$hash)) { for my $feature ( @{$v} ) { print "($k, $feature)<br>"; } } } } }

Replies are listed 'Best First'.
Re: AoHoAoH... am I delusional?
by NetWallah (Canon) on Feb 08, 2012 at 04:45 UTC
    Assuming your problem is with the ugliness of the structure, and complexity of data entry, YAML may help.

    Here is code that reverses what your code does:

    use strict; use warnings; use YAML; use Data::Dumper; my $stuff = <<YAML; --- Baked Goods: - Cookies: - Chocolate Chip - Oreo Fruits: - Apples: - Fuji - Granny Smith - Oranges: - Tangerine - Navel YAML my $perl_structure = Load $stuff; print Dumper \$perl_structure;
    this prints:
    $VAR1 = \{ 'Baked Goods' => [ { 'Cookies' => [ 'Chocolate Chip', 'Oreo' ] } ], 'Fruits' => [ { 'Apples' => [ 'Fuji', 'Granny Smith' ] }, { 'Oranges' => [ 'Tangerine', 'Navel' ] } ] };
    Update: You can use recursive printing to step through the structure, such as the code below:
    printit ($perl_structure); sub printit{ my $this = shift; if (ref $this eq ""){ print " $this "; return; } for my $k (keys %$this){ print "$k\t:"; printit($_) for @{$this->{$k}}; print "\n"; } print "\n"; }

                “PHP is a minor evil perpetrated and created by incompetent amateurs, whereas Perl is a great and insidious evil perpetrated by skilled but perverted professionals.”
            ― Jon Ribbens

Re: AoHoAoH... am I delusional?
by planetscape (Chancellor) on Feb 08, 2012 at 01:36 UTC
Re: AoHoAoH... am I delusional?
by VinsWorldcom (Prior) on Feb 08, 2012 at 02:05 UTC

    Perl has several modules under the DBI / DBD namespace to interface with all sorts of databases. Looking at your previous posts, I still can't determine what problem you're trying to solve, but it seems like highly structured data is part of it and that - to me - means a database. Perl is more than capable of scripting information to and from your choice of database.

    Pick a database - mysql for Linux, MS Access on Windows, whatever; create the schema, put your data in and let Perl pull it out (or populate) with SQL statements.

Re: AoHoAoH... am I delusional?
by BrowserUk (Patriarch) on Feb 08, 2012 at 01:20 UTC
    Note... the order in which the data is accessed is important!

    What defines the order of the data?


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

    The start of some sanity?

Re: AoHoAoH... am I delusional?
by tobyink (Canon) on Feb 08, 2012 at 06:54 UTC

    Drop the extra arrays you're currently using to preserve order, and then use Tie::IxHash.

Re: AoHoAoH... am I delusional?
by lutok (Scribe) on Feb 08, 2012 at 18:18 UTC

    Tinkering a bit with the code in the original post

    use strict; use warnings; my @fooFeatures = ( { 'Baked Goods' => [ { 'Cookies' => ['Chocolate Chip', 'Oreo'] }, ]}, { 'Fruits' => [ { 'Apples' => ['Fuji', 'Granny Smith'] }, { 'Oranges' => ['Tangerine', 'Navel'] }, ]}, ); for my $fooSet ( @fooFeatures ) { while (my ($key, $value) = each(%$fooSet)) { for my $hash ( @{$value} ) { while (my ($k, $v) = each(%$hash)) { for my $feature ( @{$v} ) { print "$key => ( $k, $feature)\n"; } } } } }

    prints out:

    Baked Goods => ( Cookies, Chocolate Chip)
    Baked Goods => ( Cookies, Oreo)
    Fruits => ( Apples, Fuji)
    Fruits => ( Apples, Granny Smith)
    Fruits => ( Oranges, Tangerine)
    Fruits => ( Oranges, Navel)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://952390]
Approved by BrowserUk
Front-paged by MidLifeXis
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (4)
As of 2024-04-25 23:57 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found