Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

A moment with Perl

by Lady_Aleena (Priest)
on Oct 06, 2007 at 22:36 UTC ( [id://643179]=perlmeditation: print w/replies, xml ) Need Help??

This morning I was working on building a table from a hash. I tried doing it on my own, but I just got confused so asked for help. Several monks swung into action to assist, bless them. About that time my beloved wanted to use the computer, so I had to move. Being a night person, I was also a bit sleepy; so when he got on the computer, I went to bed. After I woke up and logged onto Perl Monks, I saw I got a solution. I put it into action after having to do a little fiddling of my own. The results were not as I expected.

I was looking at the results and wondering why they were the way they are. I thought I had a hash of arrays, so that is what I called it when getting the above help. The words "array" and "sort" were floating around my mind. That is when I remembered reading that arrays are sorted.

Okay, so not only did my hash keys get sorted so did my array values, but then another thing hit me, they are not arrays at all. They are hashes just without the value names and fat commas.

That made me think about what I really wanted out of the script I am attempting to write. I realized that this was not just a simple hash of hashes. It needs to be a hash of hashes of arrays.

All of the above thinking took place in less than a minute after I got the results. I was a little tickled with myself for figuring this out on my own. I usually run to the chatterbox as if the sky were falling.

So, here I sit; knowing what I wanted this to be; and not knowing how to make it work as is, if that is even possible. I have already begun researching it, but any pointers of where to look or what search terms to use would be appreciated.

Lady Aleena

"An it harm none, do as ye will."

Replies are listed 'Best First'.
Re: A moment with Perl
by liverpole (Monsignor) on Oct 07, 2007 at 00:08 UTC
    Hi Lady_Aleena,

    If you haven't done so already, take a look at perldata.

    Also, (again, if this is new to you), look into using Data::Dumper.  It's a fantastic, simple way of verifying exactly what a given data structure looks like.

    For example:

    use strict; use warnings; use Data::Dumper; my $pdata = { 'red' => [ 'abc', 'def', 'ghi' ], 'blue' => { '123' => '456', 'xxx' => 'yyy' }, }; print "Dump of pdata = ", Dumper($pdata), "\n";

    which prints:

    Dump of pdata = $VAR1 = { 'blue' => { '123' => '456', 'xxx' => 'yyy' }, 'red' => [ 'abc', 'def', 'ghi' ] };

    And that tells you that you have a hash ($pdata) containing two keys, 'blue' and 'red', where 'blue' is a pointer to a hash containing two keys and two values, and 'red' is a pointer to an array containing 3 values.


    s''(q.S:$/9=(T1';s;(..)(..);$..=substr+crypt($1,$2),2,3;eg;print$..$/

      liverpole;

      I will take a closer look at perldata in a little bit. I have already looked into Data::Dumper a little bit. I do have to figure out how to write my HoHoA, but I will get there, some day, hopefully, soon.

      Thank you for stopping by and have a wonderful day!

      Lady Aleena

      "An it harm none, do as ye will."

    A reply falls below the community's threshold of quality. You may see it by logging in.
Re: A moment with Perl
by McDarren (Abbot) on Oct 07, 2007 at 04:28 UTC
    Howdy :)

    I endorse what my friend says about Data::Dumper. Extremely useful. Although, personally I like to use Data::Dumper::Simple - almost the same thing, but just a little easier to use and the output is slighly more user-friendly. Some people may recommend against this as it uses source-filtering, but my view is that for testing it is fine - I just make sure it never makes it into any production code.

    The other thing that you may find useful is the Perl Data Structures Cookbook (PDSC).

    And, if you are prepared to spend a few dollars, then Intermediate Perl is one of the best books (IMHO) for an introduction to references and data structures.

    Hope this helps,
    Darren :)

      McDarren;

      I will look at Data::Dumper::Simple sometime soon though Data::Dumper isn't all that bad. I am not sure I understand what source-filtering is. There is a lot that I will have to remember to take out my my code when the scripts are done. The PDSC looks interesting, I will look into that more closely. As for buy that book, I think I need to get a beginners' book first, when we can afford it, instead of an intermediate one. When things are better, and the next time I am in the area of a bookstore, I will look into buying one.

      Have a wonderful day...

      Lady Aleena

      "An it harm none, do as ye will."

Re: A moment with Perl
by Zaxo (Archbishop) on Oct 07, 2007 at 05:39 UTC

    You're describing a deep data structure. There is organization implied by that, and I think that you may benefit from an OO approach that modularizes data access. If a chunk of data appears to be similar to all the others at its level, it is an object and deserves a module.

    After Compline,
    Zaxo

      Zaxo;

      Could you possibly point me to some OO examples? I would be very interested in looking them over to see if they apply to what I was working on above.

      Have a gorgeous day....

      Lady Aleena

      "An it harm none, do as ye will."

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (3)
As of 2024-12-06 05:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (39 votes). Check out past polls.